如何调用ViewController的方法来显示第二个视图?
//
// MyGameViewController.h
//
#导入< UIKit/UIKit.h >
#导入“SecondViewController.h”@interface MyGameViewController : UIViewController {
<小时>
IBOutlet SecondViewController *secondViewController;
}
-(IBAction)goToSecondView;
@结束//
// MyGameViewController.m
//
#import“MyGameViewController.h”@implementationMyGameViewController
-(IBAction)goToSecondView{
<小时>
[自我呈现ModalViewController:第二个ViewController动画:是];
}//
// MyGameView.h
//
#导入< UIKit/UIKit.h >
#导入“Sprite.h”@interface MyGameView : UIView {…}
目前,我已经在 MyGameView.xib 上实现了一个按钮来调用 secondaryViewController 视图,并且它可以工作。但我希望在发生中断时通过在 MyGameView.m 内编程来调用 secondaryViewController,而不是通过按下按钮。因此,我认为有两种方法:
a) 使 goToSecondView 方法可用于 MyGameView.m
b) 将MyGameViewController.h和MyGameViewController.m中的所有代码实现到MyGameView.m中。
问题:
1)当试图使a)发生时,我必须使goToSecondView方法以(void)开头,而不是(IBAction)。那么如何在 MyGameView.m 中调用它呢?
2)我尝试执行b)并将所有代码实现到MyGameView.m。但是presentModalViewController是ViewController的一个方法,在UIView中不起作用。那么解决办法是什么呢?
//
// MyGameViewController.h
//
#import < UIKit/UIKit.h >
#import "SecondViewController.h"@interface MyGameViewController : UIViewController {
IBOutlet SecondViewController *secondViewController;
}
-(IBAction)goToSecondView;
@end//
// MyGameViewController.m
//
#import "MyGameViewController.h"@implementation MyGameViewController
-(IBAction)goToSecondView{
[self presentModalViewController:secondViewController animated:YES];
}//
// MyGameView.h
//
#import < UIKit/UIKit.h >
#import "Sprite.h"@interface MyGameView : UIView {…}
Currently I have implemented a button on the MyGameView.xib to invoke the secondViewController view and it works. But I want the secondViewController get invoked by programming inside MyGameView.m when there is interruption, not by pressing a button. Therefore, I think there are 2 approaches:
a) Either make the goToSecondView method available to MyGameView.m
b) Implement all the code in MyGameViewController.h and MyGameViewController.m to MyGameView.m.
Issues:
1) When tried to make a) happen, I have to make goToSecondView method starting with (void), not (IBAction). But then how to invoke it in MyGameView.m?
2) I tried to do b) and implemented all code to MyGameView.m. But presentModalViewController is a method of ViewController and does not work in UIView. So what is the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所说,您无法在 UIView 类中调用presentModalViewController。这似乎是使用委托的好机会。您可以执行以下操作:
在 MyGameView.h
在 MyGameView.m 中,当您需要显示第二个视图时:
在 MyGameViewController.h 中:
在 MyGameViewController.m 中:
请注意,您还需要将 MyGameViewController 设置为MyGameView 的委托。您可以在 Interface Builder 中或在代码中执行此操作,具体取决于创建两个对象的位置。
要在代码中执行此操作,例如在 MyGameViewController.h viewDidLoad 方法中:
As you stated, you can't call presentModalViewController in a UIView class. This seems like a great opportunity to use a delegate. You could do something along the lines of:
In MyGameView.h
In MyGameView.m, when you need to show the second view:
In MyGameViewController.h:
In MyGameViewController.m:
Note that you'll also need to set MyGameViewController to be the delegate of MyGameView. You could do that in Interface Builder, or in code, depending on where you create the two objects.
To do it in code, for example in the MyGameViewController.h viewDidLoad method: