How to return to the main view in a tab bar application
In a tab bar application I made a view controller for the second view. I put in an IBOutlet that is attached to a button. When the button is pressed, I want to return to the main view. This is wha开发者_StackOverflow中文版t I put, but it crashes.
-(IBAction) cancel
{
[self.view removeFromSuperview];
}
How should this be changed?
If I understand you correctly you have an application based on or similar to the "Tab Bar Application" template, and on the second tab you want to have a button to move to the first tab.
You want to do something like
tabBar.selectedItem = 0;
You probably need to connect the Cancel button to the Tab Bar controller itself, if this is not already the case.
EDIT: Here is an example, assuming your application is called "MyApp"
In class MyAppDelegate:
- (IBAction) cancel
{
tabBarController.selectedItem = 0;
}
Drag the selector from the button to the "cancel" action of the app delegate.
精彩评论