melih keskin
melih keskin

Reputation: 13

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 ?

Upvotes: 1

Views: 2953

Answers (4)

travdu
travdu

Reputation: 331

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

Upvotes: 0

Shekhar Gupta
Shekhar Gupta

Reputation: 6218

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

Upvotes: 0

Tejas Ardeshna
Tejas Ardeshna

Reputation: 4371

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

Upvotes: 0

Kumar
Kumar

Reputation: 1942

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.

Upvotes: 1

Related Questions