[英]iOS: Loading second view controller without nib file
我有一个项目,该项目由标准AppDelegate
和ViewController
文件组成,但没有使用nib
文件。
这是代码:
委托头
// AppDelegate.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *vc1;
@end
委托执行
// AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.vc1 = [[ViewController alloc] init];
self.window.rootViewController = self.vc1;
[self.window makeKeyAndVisible];
return YES;
}
@end
查看控制器头
// ViewController.h
#import <UIKit/UIKit.h>
@interface view1 : UIViewController{
UIButton *button;
}
@end
查看控制器的实现
// ViewController.m
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect buttonFrame = CGRectMake(30, 30, 100, 30);
button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: @"Switch View" forState: UIControlStateNormal];
[self.view addSubview: button];
[button addTarget: self action: @selector(buttonClicked:) forControlEvents: UIControlEventTouchDown];
}
- (void) buttonClicked: (id)sender {
SecondViewController *vc2 = [[SecondViewController alloc] init];
//both methods throw the same error - no known class method for selector
//[self presentViewController:vc2 animated:YES completion:nil];
[self presentModalViewController:vc2 animated:YES completion:nil];
}
@end
当试图加载另一个nib
稀少view controller
(在上面的方法buttonClicked),它保持引发错误
选择器'presentViewController'的未知类方法
我究竟做错了什么?
正确的方法如下:
[self presentViewController:vc2 animated: YES completion:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.