I have added a navigation controller in iphone app from my home screen it takes to a tableview from the table view it takes to the detail.I am able to navigate between the views back and forth.what I want is navigate directly from detail view to my home screen.Is there any possibility to do that?
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.hidesBackButton = TRUE;
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(save)]; self.navigationItem.leftBarButtonItem = saveButton;
id1 = [[NSUserDefaults standardUserDefaults] valueForKey:#"id1"];
theTitle = [[NSUserDefaults standardUserDefaults] valueForKey:#"theTitle"];
self.title=theTitle;
NSLog(#"hi %#", id1);
hello=#"hello";
NSLog(#"ahhhha sdfgs :%#",theTitle);
urlAddress = [NSString stringWithFormat:#"http://dev-parkguiden.knutpunkten.se/Api/GetPark?parkid=%#",self.id1];
baseURL =[[NSURL URLWithString:urlAddress]retain];
jsonData=[NSData dataWithContentsOfURL:baseURL];
NSDictionary *items=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
boom=[items objectForKey:#"description"];
latitude=[items objectForKey:#"latitude"];
longitude=[items objectForKey:#"longitude"];
NSLog(#"retard:%#",latitude);
NSLog(#"retard:%#",longitude);
NSString *html = [NSString stringWithFormat:#"<html><body><p>%#</p></body></html>", boom];
[self.webview loadHTMLString:html baseURL:nil];
[[NSUserDefaults standardUserDefaults] setValue:latitude forKey:#"lat"];
[[NSUserDefaults standardUserDefaults] setValue: longitude forKey:#"lot"];
[[NSUserDefaults standardUserDefaults] setValue: theTitle forKey:#"title"];
}
[self.navigationController popToRootViewControllerAnimated:YES];
this will pop back to the rootviewController of navigation controller
you have to just set custom back button and set button click event
// hide back button first
self.navigationItem.hidesBackButton = TRUE;
/// Add new Back Button
UIButton *back = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
back.frame = CGRectMake(0, 0, 50, 32);
[back setShowsTouchWhenHighlighted:TRUE];
[back setBackgroundImage:[UIImage imageNamed:#"backBtn.png"] forState:UIControlStateNormal];
[back addTarget:self action:#selector(backBtnTap:) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.leftBarButtonItem = backBtn;
/// Press button event to go to root or say home page
-(void)backBtnTap:(id)sender{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Related
I am trying to learn to use dispatch_get_global_queue to perform a web call asynchronously. Basically, I need to update the User Interface depending on the user's location. I started using dispatch_get_global_queue because the user interface was unresponsive while the web call was performed.
Now, my problem is that the buttons do not appear correctly when buttonUpdate is called. One button is supposed to be black, and the other is green. However, now they both appear the default blue. Also, the words/title of the buttons do not appear until I click on one of them. Anyone know why? Thank you!
I have this method called preButtonUpdate that calls the buttonUpdate method asynchronously:
-(void)preButtonUpdate {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
[self buttonUpdate];
});
}
The preButtonUpdate method is called in viewDidAppear and appReturnsActive:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self preButtonUpdate];
//other code
}
- (void)appReturnsActive {
[myTimer invalidate];
myTimer = nil;
//ONLY WANT TO PERFORM THE UPDATE IF VIEWING THE PROFILE VIEW CONTROLLER
UIViewController *currentVC = self.navigationController.visibleViewController;
NSString * name = NSStringFromClass([currentVC class]);
if ([name isEqualToString:#"ProfileViewController"]) {
[self preButtonUpdate];
}
}
Then, the preButtonUpdate calls the buttonUpdate method, which performs the web query and updates the user interface:
-(void) buttonUpdate {
[locationManagerProfile stopUpdatingLocation];
NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLatitude];
NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLongitude];
NSString *placeLatitude = [[NSUserDefaults standardUserDefaults]
stringForKey:#"savedLatitude"];
NSString *placeLongitude = [[NSUserDefaults standardUserDefaults]
stringForKey:#"savedLongitude"];
NSString *distanceURL = [NSString stringWithFormat:#"http://www.website.com/page.php?
lat1=%#&lon1=%#&lat2=%#&lon2=%#",userLatitude, userLongitude, placeLatitude,
placeLongitude];
NSData *distanceURLResult = [NSData dataWithContentsOfURL:[NSURL
URLWithString:distanceURL]];
NSString *distanceInFeet = [[NSString alloc] initWithData:distanceURLResult
encoding:NSUTF8StringEncoding];
if ([distanceInFeet isEqualToString:#"1"]) {
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:#"Button A"
style:UIBarButtonItemStyleBordered target:self action:#selector(actionA)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor
colorWithRed:44.0/255.0 green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIBarButtonItem *btnGoTwo = [[UIBarButtonItem alloc] initWithTitle:#"Button B"
style:UIBarButtonItemStyleBordered target:self action:#selector(actionB)];
self.navigationItem.rightBarButtonItem = btnGoTwo;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btnGo,
btnGoTwo, nil];
}
if ([distanceInFeet isEqualToString:#"0"]) {
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:#"Button C"
style:UIBarButtonItemStyleBordered target:self action:#selector(actionC)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor
colorWithRed:44.0/255.0 green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIBarButtonItem *btnGoTwo = [[UIBarButtonItem alloc] initWithTitle:#"Button B"
style:UIBarButtonItemStyleBordered target:self action:#selector(actionB)];
self.navigationItem.rightBarButtonItem = btnGoTwo;
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btnGo,
btnGoTwo, nil];
}
}
UI Update is always done on the main thread. So keep your UI update code out of the despatch queue
Keep [self buttonUpdate]; out of the dispatch_async block
-(void)preButtonUpdate {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
[self buttonUpdate];
});
}
Unblocking Your User Interface - Apple Developer
EDIT:
Put the if condtion code just outside the dispatch_async block
I have a modalviewcontroller and have two buttons on it: Cancel and Save. There is a UITextField which is editable.
Whenever I click on save button I do save it but the text doesnot get save because when I click on the button to open the modalviewcontroller, the text disappears. Dont know whats wrong with my code.
Here is my code :
- (void)viewWillAppear:(BOOL)animated {
self.cancel = self.navigationItem.leftBarButtonItem;
self.save = self.navigationItem.rightBarButtonItem;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancelAction)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveAction)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
[super viewWillAppear:animated];
}
-(IBAction) cancelAction{
[[self parentViewController] dismissModalViewControllerAnimated:YES];
}
-(IBAction) saveAction{
NSString *text = [textFieldBeingEdited text];
[textFieldBeingEdited setText:text];
[self setDescription:text];
[[self parentViewController] dismissModalViewControllerAnimated:YES];
}
I am not sure if I have to use the following code to save the text in textfield :
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self dismissModalViewControllerAnimated:YES];
}
I don't see in your code anywhere that you are setting the text in the UITextField when the view appears. You can add some code to your viewWillAppear method to set the text when the view appears, such as:
[textField setText:[self description]];
(It looks like you are storing the NSString in the description variable.)
Also, if this is some kind of string that could be considered a User Default, you can store the string there. I've used this before and it populates my UITextField nicely.
Code Example:
-(void)viewDidLoad
{
stringValue = [[NSUserDefaults standardUserDefaults] stringForKey:#"string"];
[textField setText:stringValue];
}
-(IBAction)saveAction
{
stringValue = [textField text];
[[NSUserDefaults standardUserDefaults] setObject:stringValue forKey:#"string"];
}
I can not understand why navigation bar is not going to add any button other than cancel button in Following code
this kind of thing done in Viber application so it is possible
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.navigationBar.tintColor=[UIColor colorWithRed:91.0/256.0 green:72.0/256.0 blue:110.0/256.0 alpha:1.0];
//picker.navigationController.navigationBar.topItem.rightBarButtonItem = nil;
[picker.navigationBar setHidden:NO];
picker.peoplePickerDelegate = self;
// Display only a person's phone, email, and birthdate
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
[NSNumber numberWithInt:kABPersonEmailProperty],
[NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
//picker.navigationBarHidden=TRUE;
picker.displayedProperties = displayedItems;
//UIBarButtonItem *addbutton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(AddContact)];
// picker.navigationController.navigationBar.topItem.rightBarButtonItem = addbutton;
[self presentModalViewController:picker animated:NO];
[picker release];
//[addbutton release];
can anybody help please
refer this article it demonstrates how to add custom button on ABPeoplePickerNavigationController
i can show you how to add custom button to navigation bar. Use this code if this is helpful,
UIImage *buttonImage = [UIImage imageNamed:#"Done.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButtonaddTarget:selfaction:#selector(navigatehome)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = aBarButtonItem;
[aBarButtonItem release];
I have a Navigation Controller app and I'm using this code to switch views:
- (IBAction)switchPage:(id)sender
{
if(self.fourthViewController == nil)
{
FourthViewController *fourthView = [[FourthViewController alloc]
initWithNibName:#"FourthView" bundle:[NSBundle mainBundle]];
self.fourthViewController = fourthView;
[fourthView release];
}
[self.navigationController pushViewController:self.fourthViewController animated:YES];
}
However I want to have a button that can go to an alternate view that is a UIWebview, My goal is to not leave the app so when user is done on the web page, the navigation controller never goes away and they can proceed to the next view.
Does anyone know of a tutorial that can help?
Why not use a modal view? Here's an example from one of my apps. So whats going on below is that I assign a credits bar button item on the nav controller nav bar which when clicked brings up the modal view controller which presents a webview. Really rather simple:
UIBarButtonItem *credits = [ [ [ UIBarButtonItem alloc ]
initWithTitle:#"More"
// initWithCustomView:testLabel
style: UIBarButtonItemStylePlain
target: self
action:#selector(credits)
]
autorelease ];
self.navigationItem.rightBarButtonItem = credits;
-(void)credits {
if (self.whyModalViewController == nil)
self.whyModalViewController = [[[ModalViewController alloc] initWithAppDelegate:self ] autorelease];
NSString *html = [ [ NSString alloc ] initWithFormat:#"%#", self.explain];
NSString *imagePath = [[NSBundle mainBundle] resourcePath];
NSLog(#"imagePath = %# ",imagePath);
imagePath = [imagePath stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
[self.whyModalViewController.webView loadHTMLString:html baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
[self presentModalViewController:whyModalViewController animated:YES];
[html release];
}
There are many ways to get back to the main view. I implemented a "back" button on the webview which dismisses the modal view controller:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(dismissAction:) forControlEvents:UIControlEventTouchUpInside];
NSString *title = #"<- Back";
button.backgroundColor = [UIColor clearColor];
[button setTitle: title forState:UIControlStateNormal];
- (IBAction)dismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
I Created Segment Control through Interface Builder.
Created a IBAction and Linked to Value Changed Option of segment Controller.
- (IBAction)GenderBttonAction:(id)sender {
printf("\n Segemt Controll");
}
When i click on segment controller this method is calling , but how would i get the seleced index value of segment controller.
Please help me dears.
((UISegmentedControl *)sender).selectedSegmentIndex;
:-)
I use the following code in a view controller.m to say launch a modal controller and it seems to work good for me.
- (void)viewDidLoad
{
NSArray *segmentContent = [NSArray arrayWithObjects:
NSLocalizedString(#"view 1", #""),
NSLocalizedString(#"view 2", #""),
NSLocalizedString(#"Close", #""),
nil];
//or images insted of text
//NSArray *segmentContent = [NSArray arrayWithObjects:
// [UIImage imageNamed:#"pic.png"],
// [UIImage imageNamed:#"spic.png"],
// [UIImage imageNamed:#"close.png"],
// nil]];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentContent];
segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment;
segmentedControl.momentary = YES; // option
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 160, 30);
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segments = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segments;
self.navigationItem.title = #"My title";
[segments release];
Then add the actions on selection, last one is a close statement if you launched a modal controller.
- (IBAction)segmentAction:(id)sender
{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
switch ([sender selectedSegmentIndex])
{
case 0:
{
// Do stuff like launch a modal controller. But don't forget to add this all into your modal controller views to get back out :)
InfoViewController *infoViewController = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
UINavigationController *aInfoViewController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
[self presentModalViewController:aInfoViewController animated:YES];
break;
}
case 1:
{
// do stuff
break;
}
case 2:
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
break;
}
}
NSLog(#"Segment clicked: %d", segmentedControl.selectedSegmentIndex);
}
Use the method below if needed to close the modal via this way.
- (IBAction)dismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
and don't forget to declare the action/method in the same respective h file.
- (IBAction)dismissAction:(id)sender;
Hope this helps.
Sincerely, Kirk