Push View Controller - 按钮动作从一个视图控制器发送到另一个视图控制器

时间:2016-02-08 20:45:28

标签: ios objective-c iphone pushviewcontroller

我正在寻找从按钮到另一个视图控制器的动作。 按下按钮时,用户将被发送到另一个视图控制器。

但是,在运行模拟器并单击按钮时,它不会转到下一个视图控制器。 (下一个视图控制器是仪表板)。

这是.h文件和.m文件

`//  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

2 个答案:

答案 0 :(得分:0)

确保您的视图控制器具有导航控制器。一种简单的方法是在AppDelegate类的applicationdidFinishLaunchingWithOptions中创建导航控制器。将您的第一个视图控制器设置为navigationController的rootViewController并将其添加到窗口。转到AppDelegate类并在那里添加此代码。

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

}

答案 1 :(得分:0)

在您的故事板中,只需右键单击(或按住控制按钮+点击按钮,然后拖动要导航的视图控制器。显示选项菜单时,选择“显示”。

同样在您的故事板中,请确保您的视图控制器已嵌入UINavigationController。 (选择您的第一个视图控制器,在Xcode菜单中单击Editor > Embed In > Navigation Controller)。

很抱歉,我立即写了这个答案,如果你需要一些图片,我可以在下次更新中附上它