简体   繁体   中英

Remove view controller from another view controller

I am very new to iPhone app development.
I am developing one example application for iPhone emulator using Objective-C++ and std CPP.

I have two views in my application, on some events from CPP code i am displaying second view using following code from the first view controller.

// Defined in .h file 
secondViewScreenController *mSecondViewScreen;

// .mm file Code gets called based on event from CPP (common interface function between Objective-C++ and CPP code)
mSecondViewScreen = [[secondViewScreenController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:mSecondViewScreen animated:YES];

I am able to see second view coming on screen, but problem is that i am unable to end/remove second view controller from first view controller.

How can i remove second view controller from first view controller using second view controller's pointer or using any other method.

To remove second view i have following code in second view controller file, which gets called on button click event of second view.

// In .mm of second view controller. 
- (IBAction)onEndBtnClicked:(UIButton *)sender
{
   [self dismissModalViewControllerAnimated:NO];
   [self.navigationController popViewControllerAnimated:YES];
}

Above code works perfectly, when i click on the seconds view's end button it removes the second view controller from the screen and navigets to first view, how can i use same code to remove second view from the first view controller.

I tied to use NSNotificationCenter to send event from first view to second view to call the function onEndBtnClicked but it is not working.

What is the proper way of doing it?

OSX version: 10.5.8 and Xcode version: 3.1.3

In the secondViewController create a protocol like:

@protocol SecondViewScreenControllerDelegate <NSObject>

- (void)secondViewScreenControllerDidPressCancelButton:(UIViewController *)viewController sender:(id)sender;

// Any other button possibilities

@end

Now you have to add a property in the secondViewController class:

@property (weak, nonatomic) id<SecondViewScreenControllerDelegate> delegate;

You sinthesize it in the secondViewController implementation:

@synthesize delegate = _delegate;

Finally all you have to do is implement the protocol in your firstViewController and set the secondViewController properly prior presenting it:

@interface firstViewController : UIViewController <SecondViewScreenControllerDelegate>

...

@implementation firstViewController

    - (void)secondViewScreenControllerDidPressCancelButton:(UIViewController *)viewController sender:(id)sender
    {
         // Do something with the sender if needed
         [viewController dismissViewControllerAnimated:YES completion:NULL];
    }

Then when presenting the secondViewController from the first:

UIViewController *sec = [[SecondViewController alloc] init]; // If you don't need any nib don't call the method, use init instead
sec.delegate = self;
[self presentViewController:sec animated:YES completion:NULL];

And ready. Whenever you want to dismiss the secondViewController from the first, just call: (inside the secondViewController implementation)

[self.delegate secondViewScreenControllerDidPressCancelButton:self sender:nil]; // Use nil or any other object to send as a sender

All that happens is that you send a pointer of the secondViewController that you can use from the first. Then you can work with it without problem. No C++ needed. In Cocoa you won't need C++. Almost everything can be done with Objective-C, and it's more dynamic.

If there are only two views in your application then use

- (IBAction)onEndBtnClicked:(UIButton *)sender
{
   [self dismissModalViewControllerAnimated:NO];
}

remove line below:

 [self.navigationController popViewControllerAnimated:YES];

as it is you are dismissing second view then why you want to remove it from first view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM