Linking One View To Another Via A Button
I'm a bit new to programming for iOS, and I'm having some trouble linking one view to another via a button. I'm just creating a simple little app that does some calculations on a NSDate in an attempt to learn XCode and iOS programming.
I've already searched this quite a bit, and I've tried to learn from other examples but I'm having trouble getting the view to present, nothing happens when I press my button (which I've already checked to be linked to the button).
I've been having trouble understanding view programming, so please bear with me.
Here's my code for my button:
-(IBAction)resultsPressed
{
TimeResults *timeResults;
timeResults = [[TimeResults alloc] initWithNibName:@"TimeResults" bundle:nil];
[self.navigationController pushViewController:t开发者_C百科imeResults animated:YES];
[timeResults release];
}
TimeResults.xib is using a Navigation Controller if it matters, while my root view is simply a view. My thinking behind this was so that I could get the "back" button (though I'm not sure if this is the correct way tot do this, since they are not a part of the same hierarchy). Any suggestions on how this should be done would be greatly appreciated!
Nothing seems wrong with the code you posted, but you should have the Navigation Controller associated with the first nib, as the back button will display by default when a new view is pushed onto the stack.
Also, make sure that the Navigation Controller
is set up properly in your AppDelegate
. The proper way to do this can be seen if you start a new project and select "Navigation-based Application". If you use the new project as a sample to show you how to set up your old project correctly, you will have to make sure that the nib
is set up correctly too. I would suggest using the new project, hooking it up as a UIViewController
instead of a UITableViewController
, and then moving your code from your old project to this new one.
Finally, make sure that you always import the .h
file of the UIViewController
you are going to push to. Hope that helps!
To load another view, try
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
TimeResults *timeResults = [[TimeResults alloc] initWithNibName:@"TimeResults" bundle:nil];
[delegate.window setRootViewController:timeResults];
Hope it works. :)
精彩评论