Seenu February 2016

Push View Controller - Button Action sending from one view controller to another

I am looking to make an action from a button to another view coontroller. When the button is pressed, the user is sent to another view controller.

However, when running the simulator and clicking the button, it is not going to the next view controller. (The next view controller is Dashboard).

Here is the .h file and .m file

`//  ViewController.h
//  Shopping List
//
//  Created by Seenu on 2/8/16.
//  Copyright © 2016. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *Login;
@property (weak, nonatomic) IBOutlet UITextField *emailTextfield;
@property (weak, nonatomic) IBOutlet UILabel *Password;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *button;

- (IBAction)SignIn:(id)sender;


@end`//

//
//  ViewController.m
//  Shopping List
//
//  Created by Seenu on 2/8/16.
//  Copyright © 2016. All rights reserved.
//

#import "ViewController.h"
#import "Dashboard.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize button;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

}
- (IBAction)SignIn:(id)sender {
Dashboard *home = [[Dashboard alloc] initWithNibName:@"Dashboard" bundle:nil];
[self.navigationController pushViewController:home animated:YES];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Answers


Shehzad Ali February 2016

Make sure your view controller have navigation controller. An easy way of doing this is create navigation controller in your applicationdidFinishLaunchingWithOptions in your AppDelegate Class. Set your first view controller as rootViewController of navigationController and add it to window. Go to your AppDelegate Class and add this code over there.

- (BOOL)application:(UIApplication *)application
 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UIViewController *rootController = 
         [[RootViewController alloc] 
         initWithNibName:@"RootViewController" bundle:nil];

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

    self.window = [[UIWindow alloc] 
        initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
    return YES;

}


Muhammad Yusuf February 2016

In your storyboard, just right click (or hold down control button + click) at your button then drag to the view controller you want to navigate. When option menu showing, select "Show".

Also in your storyboard, please ensure that your view controller has embedded in a UINavigationController. (Select your first view controller, in Xcode menu click Editor > Embed In > Navigation Controller).

Sorry, I wrote this answer instantly, if you need some picture, I can attach it in the next update.

Post Status

Asked in February 2016
Viewed 2,192 times
Voted 9
Answered 2 times

Search




Leave an answer