【问题标题】:iOS: Loading second view controller without nib fileiOS:加载没有 nib 文件的第二个视图控制器
【发布时间】:2015-05-17 11:06:28
【问题描述】:

我有一个项目,它包含标准的AppDelegateViewController 文件,但不使用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-less view controller (在上面的buttonClicked方法中)时,它一直抛出错误

选择器“presentViewController”没有已知的类方法

我做错了什么?

【问题讨论】:

    标签: ios objective-c uiviewcontroller uikit xib


    【解决方案1】:

    正确的方法如下:

    [self presentViewController:vc2 animated: YES completion:nil];
    

    【讨论】:

    • 有效!非常感谢你。这有效,因为它在一个方法中 - 是否可以在普通函数中运行它?
    • 正常功能是什么意思?
    • 我刚试过,它也有效。谢谢你。我的意思是使用函数而不是 Objective-C 方法: (void) buttonClicked {...} 而不是 (void) buttonClicked: (id)sender {...}我>
    猜你喜欢
    • 2016-09-25
    • 2012-08-12
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多