简体   繁体   中英

pushviewcontroller doesn't work?

There is a button at the viewcontroller and when i click the button pushviewcontroller doesn't work.


my appdelegate.m file :

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

Button click method (in ExampleViewController):

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

How i can fire button click event ?

Update your AppDelegate.m file with following code

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.

you need to change in appDelegate.m, need to set navigation controller as root view.

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

You need to first add your ExampleViewController to UINavigationController as below

ExampleViewController *exampleViewController = [ExampleViewController new];

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

self.window.rootViewController = exampleViewController;

After this you can do

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

Swift version of what Latit mentioend:

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

more info: http://www.ryanwright.me/cookbook/swift/ui-library/uinavigationcontrolle/set-root-controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM