2012-12-24 147 views
1

我对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]; 

我能看到第二视图上的屏幕来了,但问题是,我无法停止/从第一视图控制器删除第二个视图控制器。

如何从第一个视图控制器使用第二个视图控制器的指针或使用任何其他方法删除第二个视图控制器。

要删除第二个视图,我有第二个视图控制器文件中的以下代码,它被第二个视图的按钮单击事件调用。

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

上面的代码工作完美,当我在秒视图的结束按钮,单击它从屏幕移开并navigets先来看第二个视图控制器,我怎么能使用相同的代码来从第一视图中删除第二视图控制器。

我绑定使用NSNotificationCenter将事件从第一个视图发送到第二个视图来调用功能onEndBtnClicked但它不起作用。

这样做的正确方法是什么?

OSX版本:10.5.8和Xcode的版本:3.1.3

+0

显示使用NSNotificationCenter时的部分。 –

回答

4

在secondViewController创建这样一个协议:

@protocol SecondViewScreenControllerDelegate <NSObject> 

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

// Any other button possibilities 

@end 

现在你必须在secondViewController类添加属性:

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

您在secondViewController实现sinthesize它:

@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]; 

并准备就绪。每当你想解雇从第一secondViewController,只要致电:(该secondViewController里面执行)

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

所发生的一切是你送,你可以从第一次使用的secondViewController的指针。然后你可以毫无问题地使用它。不需要C++。在Cocoa中,你不需要C++。几乎所有的东西都可以用Objective-C完成,而且更加动态。

+0

感谢您的回复,我会尝试您的建议并让您知道结果。我正在使用Objective-C的CPP,因为我有原生(CPP)代码,它具有我应用程序的所有基本功能,我想重复使用它。 – User7723337

+0

它工作吗?请回答:) –

+0

我在添加代码后出现编译错误,添加“@property(weak,nonatomic)id delegate后出现错误;”到secondViewController.h文件。你能为此提出一个完整的例子,因为我不知道在哪里准确地添加上面的代码行。我想我可能会犯一些错误。 – User7723337

0

如果只有两个在你的应用程序的意见,然后用下面

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

删除线:

[self.navigationController popViewControllerAnimated:YES]; 

因为它是你驳回第二个观点,那么你为什么要从第一个视图中删除它。

+0

“为什么你想从第一个视图中删除它”,在CPP代码的一些事件中,我想要删除第二个视图,那些事件在第一个视图控制器中被接收,因此我需要从第一个视图控制器中删除第二个视图。 – User7723337

相关问题