Xcode 错误:不兼容的 Objective-C 类型。预期的“struct UIView”

发布于 2024-10-20 15:38:15 字数 1415 浏览 11 评论 0原文

我正在启动一个具有两个视图的多视图应用程序:NewGame 和 Players。我以为我已经正确设置了一切,但显然不是。

MainViewController.h

#import <UIKit/UIKit.h>

@class NewGame; @class Players;

@interface MainViewController : UIViewController {
    IBOutlet NewGame *newGameController;
    IBOutlet Players *playersController;
}

-(IBAction) loadNewGame:(id)sender;
-(IBAction) loadPlayers:(id)sender;

-(void) clearView;

@end

MainViewController.m

#import "MainViewController.h"
#import "NewGame.h"
#import "Players.h"

@implementation MainViewController

-(IBAction) loadNewGame:(id)sender {
    [self clearView];
    [self.view insertSubview:newGameController atIndex:0];
}

-(IBAction) loadPlayers:(id)sender {
    [self clearView];
    [self.view insertSubview:playersController atIndex:0];
}

-(void) clearView {
    if (newGameController.view.superview) {
        [newGameController.view removeFromSuperview];
    } else if (playersController.view.superview) {
        [playersController.view removeFromSuperview];
    }
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [self loadNewGame:nil];
    [super viewDidLoad];
}

一些图像...

https://i.sstatic.net/GwXMa.png https://i.sstatic.net/XHktH.png

I am starting a multiview app with two views: NewGame and Players. I thought I was setting everything up properly, but apparently not.

MainViewController.h

#import <UIKit/UIKit.h>

@class NewGame; @class Players;

@interface MainViewController : UIViewController {
    IBOutlet NewGame *newGameController;
    IBOutlet Players *playersController;
}

-(IBAction) loadNewGame:(id)sender;
-(IBAction) loadPlayers:(id)sender;

-(void) clearView;

@end

MainViewController.m

#import "MainViewController.h"
#import "NewGame.h"
#import "Players.h"

@implementation MainViewController

-(IBAction) loadNewGame:(id)sender {
    [self clearView];
    [self.view insertSubview:newGameController atIndex:0];
}

-(IBAction) loadPlayers:(id)sender {
    [self clearView];
    [self.view insertSubview:playersController atIndex:0];
}

-(void) clearView {
    if (newGameController.view.superview) {
        [newGameController.view removeFromSuperview];
    } else if (playersController.view.superview) {
        [playersController.view removeFromSuperview];
    }
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [self loadNewGame:nil];
    [super viewDidLoad];
}

A couple of images...

https://i.sstatic.net/GwXMa.png
https://i.sstatic.net/XHktH.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

⊕婉儿 2024-10-27 15:38:16

我认为playerController是视图控制器。然后添加

[self.view addSubview: playerController.view];

或如果没有则将它们子类化为 UIView

I suppose playerController is view controller. Then add

[self.view addSubview: playerController.view];

OR if not then subclass them for UIView

夜血缘 2024-10-27 15:38:16

NewGame 和 Players 都需要继承 UIView。如果它们是 ViewController,而不是 UIView,则需要使用 newGameController.view 而不是 newGameController。

NewGame and Players both need to subclass UIView. If they're ViewControllers, not UIViews, you'll need to use newGameController.view instead of newGameController.

伪装你 2024-10-27 15:38:15

视图是代表屏幕上显示内容的对象。 视图控制器是执行与这些视图相关的应用程序逻辑的对象。视图层次结构是视图的集合。您正在尝试将视图控制器添加到视图层次结构中,就好像它实际上是一个视图一样。

粗略地说,您的应用程序的每个“屏幕”都应该有一个视图控制器。该视图控制器可以管理任意数量的视图。其主视图可通过其 view 属性访问。

让应用程序运行的一个快速修复方法是添加视图控制器的主视图,而不是视图控制器本身。因此,例如,这样:

[self.view insertSubview:playersController atIndex:0];

...将变成这样:

[self.view insertSubview:playersController.view atIndex:0];

话虽如此,从长远来看,这不是一个好的解决方案,您应该研究一种更结构化的方式来组织从视图控制器到视图控制器的转换。 UINavigationController 对于初学者来说是一个不错的选择。

Views are objects that represent what appears on screen. View controllers are objects that perform application logic relating to those views. A view hierarchy is a collection of views. You are attempting to add a view controller to a view hierarchy as if it were actually a view.

Roughly speaking, you should have one view controller for every "screen" of your app. This view controller can manage any number of views. Its main view is accessible through its view property.

A quick fix to get your application operational would be to add the main view of your view controllers instead of the view controllers themselves. So, for example, this:

[self.view insertSubview:playersController atIndex:0];

...would become this:

[self.view insertSubview:playersController.view atIndex:0];

Having said that, this is not a good solution long-term, and you should investigate a more structured way of organising transitions from view controller to view controller. UINavigationController is a good option for beginners.

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