Tunvir Rahman Tusher
Tunvir Rahman Tusher

Reputation: 6631

viewController and pushViewcontroller not working?

I am trying pushViewController in iOS.But the execution result is following

My Fist view

Second View

When i hit the button it will make a transition from view A to View B. Here is my code for view A

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

@interface ViewControllerA : UIViewController

-(IBAction)next:(id)sender;

@end

in .m file

#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

in viewController B

@interface ViewControllerB : UIViewController

@property (nonatomic,strong)NSString *string;

@end

In .m file

#import "ViewControllerB.h"

@interface ViewControllerB ()

@end

@implementation ViewControllerB

@synthesize string=_string;

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

In want to set the string in viewB to "tunvir" and load the object as viewB. but a lot of warning appears.Why this is happening and how to fix this?Thanks

Upvotes: 0

Views: 8030

Answers (5)

Anil Varghese
Anil Varghese

Reputation: 42977

If you are using storyboard do like this..

-(IBAction)next:(id)sender 
{

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

Dont forget to give StoryboardId for the viewControllerB as viewControllerB

Upvotes: 3

Bonnie
Bonnie

Reputation: 4953

if you only need to set the title for the navigationBar of the ViewControllerB'

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"Title for NavigationBar";
}

EDIT use this if ur using Storyboard

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

Upvotes: 1

Lithu T.V
Lithu T.V

Reputation: 20021

Yo uare not initialising properly .Use

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

instead of

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

Upvotes: 0

BhushanVU
BhushanVU

Reputation: 3455

try below code...

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

Upvotes: 0

Nirav Gadhiya
Nirav Gadhiya

Reputation: 6342

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

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

Upvotes: 0

Related Questions