pushviewcontroller不起作用?

时间:2015-06-23 06:11:11

标签: ios objective-c pushviewcontroller

viewcontroller上有一个按钮,当我点击 时,按钮 pushviewcontroller 不起作用。

我的 appdelegate.m 文件:

- (BOOL)applicationUIApplication *)application **didFinishLaunchingWithOptionsNSDictionary** *)launchOptions 
{
    self.window = [[UIWindow alloc] initWithFrame:EK_SCREEN_BOUNDS];

    ExampleViewController *exampleViewController = [ExampleViewController new];

    self.window.rootViewController = exampleViewController;

   [self.window makeKeyAndVisible];

   return YES;
}

按钮单击方法(在ExampleViewController中):

mainViewController *frm = [mainViewController new];
[self.navigationController pushViewController:frm animated:YES];

如何触发按钮点击事件?

4 个答案:

答案 0 :(得分:1)

使用以下代码

更新AppDelegate.m文件
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ExampleViewController *exampleViewController = [ExampleViewController new];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:exampleViewController];
self.window.rootViewController = navController;
//self.window.backgroundColor = [UIColor lightGrayColor];
[self.window makeKeyAndVisible];
  

问题是你没有初始化rootViewController   导航控制器。因此,当您按下视图控制器时   导航控制器,它没有工作。因为你永远不会初始化   导航控制器。

答案 1 :(得分:0)

你需要在appDelegate.m中进行更改,需要将导航控制器设置为root视图。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc] initWithFrame:EK_SCREEN_BOUNDS];

ExampleViewController *exampleViewController = [ExampleViewController new];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:exampleViewController];
self.window.rootViewController = navigation;

[self.window makeKeyAndVisible];

return YES;
}

答案 2 :(得分:0)

您需要先将ExampleViewController添加到UINavigationController,如下所示

ExampleViewController *exampleViewController = [ExampleViewController new];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:exampleViewController];

self.window.rootViewController = exampleViewController;

在此之后你可以做到

mainViewController *frm = [mainViewController new]; [self.navigationController pushViewController:frm animated:YES];

答案 3 :(得分:0)

Latt mentioend的

Swift 版本:

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let screenBounds:CGRect = UIScreen.mainScreen().bounds

        var viewController:UIViewController = ViewController();

        self.window = UIWindow(frame: screenBounds);
        var nav:UINavigationController = UINavigationController(rootViewController: viewController);

        self.window?.rootViewController = nav;
        self.window?.makeKeyAndVisible();
       return true
    }
}

更多信息: http://www.ryanwright.me/cookbook/swift/ui-library/uinavigationcontrolle/set-root-controller