viewController和pushViewcontroller无法正常工作?

时间:2013-05-14 09:38:04

标签: ios viewcontroller

我在iOS中尝试pushViewController。但是执行结果是

My Fist view

Second View

当我点击按钮时,它将从视图A转换到视图B. 这是我的视图A的代码

#import <UIKit/UIKit.h>
#import "ViewControllerB.h"

@interface ViewControllerA : UIViewController

-(IBAction)next:(id)sender;

@end
<。>文件中的

#import "ViewControllerA.h"
@interface ViewControllerA ()
@end

@implementation ViewControllerA
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(IBAction)next:(id)sender
{
    ViewControllerB *viewController=[[ViewControllerB alloc]init];
    viewController.string=@"tunvir";
    [self.navigationController pushViewController:viewController animated:YES];
}
@end
viewController B中的

@interface ViewControllerB : UIViewController

@property (nonatomic,strong)NSString *string;

@end

在.m文件中

#import "ViewControllerB.h"

@interface ViewControllerB ()

@end

@implementation ViewControllerB

@synthesize string=_string;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
@end

想要将viewB中的字符串设置为“tunvir”并将对象加载为viewB。但是出现了很多警告。为什么会发生这种情况以及如何解决这个问题?谢谢

5 个答案:

答案 0 :(得分:3)

如果您正在使用故事板,请这样做..

-(IBAction)next:(id)sender 
{

ViewControllerB *viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"viewControllerB"];
viewController.string=@"tunvir";
[self.navigationController pushViewController:viewController animated:YES];
}  

别忘了将StoryboardId作为viewControllerB

提供给viewControllerB

答案 1 :(得分:1)

如果您只需要为ViewController'

的导航栏设置标题
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"Title for NavigationBar";
}

编辑如果您使用Storyboard

,请使用此功能
-(IBAction)next:(id)sender
{
UIStoryboard *storyboard = [[UIStoryboard storyboardWithName:@"MainStoryboard" 
                              bundle:NULL]
UIViewController *viewControllerb = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"];
[[self navigationController] pushViewController:viewControllerb animated:YES];
}

答案 2 :(得分:0)

替换ViewControllerB *viewController=[[ViewControllerB alloc]init];
ViewControllerB *viewController=[[ViewControllerB alloc] initWithNibName:@"ViewControllerB" bundle:nil];

答案 3 :(得分:0)

尝试以下代码...

 -(IBAction)next:(id)sender
    {
        ViewControllerB *viewController=[[ViewControllerB alloc]initWithNibName:@"ViewControllerB" bundle:nil];
        viewController.string=@"tunvir";
        [self.navigationController pushViewController:viewController animated:YES];
    }

答案 4 :(得分:0)

哟你没有正确初始化。使用

ViewControllerB *viewController=[[ViewControllerB alloc] initWithNibName:@"ViewControllerBXIbFileNAme" bundle:nil];

而不是

ViewControllerB *viewController=[[ViewControllerB alloc]init];