pushviewcontroller 不起作用?

pushviewcontroller doesn't work?

在 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];

如何触发按钮点击事件?

使用以下代码更新您的 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];

The problem is that you are not initializing rootViewController from navigation controller. So, when you push a view controller on navigation controller it doesn't work. Because you never initialize a navigation controller.

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

- (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;
}

您需要先将 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];

Swift Latitude 提到的版本:

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