从另一个视图控制器移除视图控制器
我对iPhone应用程序开发很新颖。
我正在开发使用Objective-C ++和std CPP的iPhone模拟器的一个示例应用程序。
我在我的应用程序中有两个视图,从CPP代码中的一些事件我显示第二个视图使用下面的代码从第一个视图控制器。
// 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];
我能够看到第二个视图在屏幕上,但问题是,我无法结束/从第一个视图控制器中删除第二个视图控制器。
如何从第一个视图控制器使用第二个视图控制器的指针或使用任何其他方法删除第二个视图控制器。
要删除第二个视图,我有第二个视图控制器文件中的第二个视图的button单击事件调用后面的代码。
// In .mm of second view controller. - (IBAction)onEndBtnClicked:(UIButton *)sender { [self dismissModalViewControllerAnimated:NO]; [self.navigationController popViewControllerAnimated:YES]; }
上面的代码完美地工作,当我点击秒视图的结束button,它将第二个视图控制器从屏幕和导航到第一个视图,我怎样才能使用相同的代码从第一个视图控制器中删除第二个视图。
我绑定使用NSNotificationCenter
发送事件从第一个视图到第二个视图调用onEndBtnClicked
函数,但它不工作。
什么是正确的做法呢?
OSX版本:10.5.8和Xcode版本:3.1.3
在第二个ViewController中创build一个协议,如:
@protocol SecondViewScreenControllerDelegate <NSObject> - (void)secondViewScreenControllerDidPressCancelButton:(UIViewController *)viewController sender:(id)sender; // Any other button possibilities @end
现在你必须在secondViewController类中添加一个属性:
@property (weak, nonatomic) id<SecondViewScreenControllerDelegate> delegate;
你在第二个ViewController实现中使用它:
@synthesize delegate = _delegate;
最后,你所要做的就是在你的firstViewController中实现协议,并在提交之前正确设置secondViewController:
@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]; }
然后当从第一个显示secondViewController:
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];
准备好了 每当你想从第一个closuressecondViewController时,只需调用:(在secondViewController实现中)
[self.delegate secondViewScreenControllerDidPressCancelButton:self sender:nil]; // Use nil or any other object to send as a sender
发生的一切就是发送第一个可以使用的secondViewController的指针。 那么你可以毫无问题地使用它。 没有C ++需要。 在Cocoa中,你不需要C ++。 几乎所有的东西都可以用Objective-C完成,而且更加dynamic。
如果您的应用程序中只有两个视图,请使用
- (IBAction)onEndBtnClicked:(UIButton *)sender { [self dismissModalViewControllerAnimated:NO]; }
删除下面的行:
[self.navigationController popViewControllerAnimated:YES];
因为它是你驳回第二个观点,那么你为什么要从第一个视图中删除它。
- 从右到左语言翻转UIImageViews
- Pod Spec Lint CoreSimulator检测到Xcode.app重定位或CoreSimulatorService版本更改
- 用SecRandomCopyBytes生成范围内的随机数
- 如何获得使用MPMediaPickerControllerselect的文件的path?
- iOS 7/8 UITableView Cell:两个具有dynamic高度的UILabel,以及可变行高的自动布局
- 将“UITableViewCell”types的值转换为自定义单元格
- 如何创建自定义表格视图单元格(快速)
- 更好的图像着色逻辑/algorithm
- 将URL上的图像附加到AFNetworking 2.0请求?