如何调用ViewController的方法来显示第二个视图?

发布于 2024-10-06 19:54:10 字数 1249 浏览 5 评论 0原文

//
// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2024-10-13 19:54:10

正如您所说,您无法在 UIView 类中调用presentModalViewController。这似乎是使用委托的好机会。您可以执行以下操作:

在 MyGameView.h

@protocol MyGameViewDelegate
- (void)showSecondView;
@end

@interface MyGameView {
}
@property (nonatomic, assign) id <MyGameViewDelegate> delegate;
...
@end

在 MyGameView.m 中,当您需要显示第二个视图时:

[self.delegate showSecondView];

在 MyGameViewController.h 中:

#import "MyGameView.h"
@interface MyGameViewController : UIViewController <MyGameViewDelegate> {
...

在 MyGameViewController.m 中:

#pragma mark MyGameViewDelegate methods

- (void)showSecondView {
    [self goToSecondView];
}

请注意,您还需要将 MyGameViewController 设置为MyGameView 的委托。您可以在 Interface Builder 中或在代码中执行此操作,具体取决于创建两个对象的位置。

要在代码中执行此操作,例如在 MyGameViewController.h viewDidLoad 方法中:

myGameView.delegate = self;

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

@protocol MyGameViewDelegate
- (void)showSecondView;
@end

@interface MyGameView {
}
@property (nonatomic, assign) id <MyGameViewDelegate> delegate;
...
@end

In MyGameView.m, when you need to show the second view:

[self.delegate showSecondView];

In MyGameViewController.h:

#import "MyGameView.h"
@interface MyGameViewController : UIViewController <MyGameViewDelegate> {
...

In MyGameViewController.m:

#pragma mark MyGameViewDelegate methods

- (void)showSecondView {
    [self goToSecondView];
}

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:

myGameView.delegate = self;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文