0

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

7
  • 1
    What kind of warnings? Commented May 14, 2013 at 9:39
  • nested push animation can result in corrupted navigation bar 2013-05-14 14:55:36.881 TestBarDelegate[1687:11303] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted. 2013-05-14 14:55:36.883 TestBarDelegate[1687:11303] Unbalanced calls to begin/end appearance transitions for <ViewControllerB: 0x7173f70>. Commented May 14, 2013 at 9:40
  • Do you have xib file for ViewControllerB??? Commented May 14, 2013 at 9:46
  • no..i am working with storyboard. Commented May 14, 2013 at 10:03
  • Your code is working. Second screen get dark because of default view background set to black. Why you are getting so many warnings? Did u initialize the navigation controller properly in AppDelegate? Commented May 14, 2013 at 10:10

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

causing exception and crash the program.
If you're using a Storyboard then why not use a segue instead of having to implement the pushViewController: yourself ...
0

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

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

2 Comments

causing exception and crash the program.
this might be because compiler cant find nib file for @"ViewControllerB"
0

try below code...

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

2 Comments

causing exception and crash the program.
do you have xib of ViewControllerB? and why you are using viewController.string=@"tunvir"; this line ?
0

Yo uare not initialising properly .Use

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

instead of

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.