Remove view controller from another controller

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

I have two views in my application, on some events from the CPP code I show the second view using the 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 see that the second view appears on the screen, but the problem is that I can not finish / delete the controller of the second view from the first controller.

How can I remove the second view controller from the first view controller using the pointer of the second view controller or using any other method.

To remove the second view, I have the following code in the second view controller file, which is called when the button of the second view is clicked.

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

The above code works fine when I click the end button for viewing seconds, it removes the second view controller from the screen and navigates to the first view, how can I use the same code to delete the second view from the first view controller.

I bound use NSNotificationCenter to send events from the first view to the second view to call the onEndBtnClicked function, but it does not work.

What is the right way to do this?

OS Version: 10.5.8 and Xcode Version: 3.1.3

+1
source share
2 answers

In secondViewController, create a protocol such as:

 @protocol SecondViewScreenControllerDelegate <NSObject> - (void)secondViewScreenControllerDidPressCancelButton:(UIViewController *)viewController sender:(id)sender; // Any other button possibilities @end 

Now you need to add the property to the secondViewController class:

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

You insert it into the secondViewController implementation:

 @synthesize delegate = _delegate; 

Finally, all you have to do is implement the protocol in your first controller and configure the second control monitor correctly before 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 second, the ViewController 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 cancel the second control controller the first time, just call: (inside the implementation of the second ViewController)

 [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 to the second control, which you can use from the first. Then you can work with it without any problems. No C ++ required. In Cocoa, you won't need C ++. Almost everything can be done using Objective-C, and it is more dynamic.

+4
source

If the application has only two types, use

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

delete the line below:

  [self.navigationController popViewControllerAnimated:YES]; 

because you reject the second view, and then why you want to remove it at a glance.

0
source

Source: https://habr.com/ru/post/980710/


All Articles