Xcode 错误:不兼容的 Objective-C 类型。预期的“struct UIView”
我正在启动一个具有两个视图的多视图应用程序: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为playerController是视图控制器。然后添加
或如果没有则将它们子类化为 UIView
I suppose playerController is view controller. Then add
OR if not then subclass them for UIView
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.
视图是代表屏幕上显示内容的对象。 视图控制器是执行与这些视图相关的应用程序逻辑的对象。视图层次结构是视图的集合。您正在尝试将视图控制器添加到视图层次结构中,就好像它实际上是一个视图一样。
粗略地说,您的应用程序的每个“屏幕”都应该有一个视图控制器。该视图控制器可以管理任意数量的视图。其主视图可通过其
view
属性访问。让应用程序运行的一个快速修复方法是添加视图控制器的主视图,而不是视图控制器本身。因此,例如,这样:
...将变成这样:
话虽如此,从长远来看,这不是一个好的解决方案,您应该研究一种更结构化的方式来组织从视图控制器到视图控制器的转换。
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:
...would become this:
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.