tags:

views:

12

answers:

1

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 what I put, but it crashes.

-(IBAction) cancel
{
    [self.view removeFromSuperview];
}

How should this be changed?

+1  A: 

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.

Krumelur
The application I made is built from the "Tab Bar Application" template. The only modification I made is I made a new view controller for the second view. The code you wrote doesn't work. What is the tab bar controller? I don't see any files with that name.
awakeFromNib
You need to access the instance of UITabBarController. There is one instance in the application generated from the template. One suggestion is to put the code in the application delegate.
Krumelur
If I add it to the delegate then how can I connect it to the button?
awakeFromNib
In Interface Builder, just drag the connection to it.
Krumelur