1

I want to Push my viewController on PresentViewController

When i click on button initially i am loading PresentViewController, here is my code.

- (IBAction)JoinClicked:(id)sender{

    JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:@"JoinWithViewController" bundle:nil];
    [self.view.window.rootViewController presentViewController:detail_view_controller
                                                      animated:YES
                                                    completion:NULL];

}

this works fine, but when i click on button which is there on PresentViewController on click i want to Push my view, but in does not pushes.

Please help, Thanks in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Krunal
  • 6,440
  • 21
  • 91
  • 155
  • :How you initialize the rootViewController in appDelegate please put your code. – TamilKing Oct 30 '13 at 06:47
  • There is nothing in `appDelegate` i am using stroyboard – Krunal Oct 30 '13 at 06:54
  • You can also use CATransition to pudh your view with animation with duration. Use framework for that. – Shreyansh Shah Oct 30 '13 at 06:54
  • do u know any example for this ? – Krunal Oct 30 '13 at 06:58
  • If even though you use storyBoard you need to do didFinishLaunchingWithOptions: self.viewController = [[viewController alloc] initWithNibName:@"viewController" bundle:nil]; UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:self.viewController]; self.window.rootViewController = nav; to push viewController – TamilKing Oct 30 '13 at 06:58
  • http://stackoverflow.com/questions/7014129/pushviewcontroller-without-navigationcontroller should adequately explain exactly a push. – Morkrom Oct 30 '13 at 06:59
  • @TamilKing: When i wrote your code in `didFinishLaunchingWithOptions` there i am unable to see my Bottom `TabBar` – Krunal Oct 30 '13 at 07:10
  • @Krunal:Are you using tab bar. OtherWise Wait I ll update my code you Create TabBar programaticaly. I ll update one sample code in my ans You refer that – TamilKing Oct 30 '13 at 07:14
  • But for some reason i have to use storyboard, – Krunal Oct 30 '13 at 07:23

2 Answers2

2
JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:@"JoinWithViewController" bundle:nil];
[self.navigationController pushViewController:detail_view_controller animated:YES];

Try like this to push viewController. If you use TabBar do like this in AppDelegate. If you create TabBar Drag and drop means leave that. Create TabBar Programatically like below.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UITabBarController *tabController = [[UITabBarController alloc]init];

    self.viewController = [[CalculatorViewController alloc] initWithNibName:@"CalculatorViewController" bundle:nil];
    UITabBarItem *tab_item1 = [[UITabBarItem alloc]init];
    //tab_item1.title = @"Calculator";
    tab_item1.image = [UIImage imageNamed:@"Calculator.png"];
    [self.viewController setTabBarItem:tab_item1];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:self.viewController];

    ShowPhoneViewController *phone = [[ShowPhoneViewController alloc]init];
    UITabBarItem *tab_item2 = [[UITabBarItem alloc]init];
    tab_item2.title = @"Latest Mobiles";
    tab_item2.image = [UIImage imageNamed:@"Mobile.png"];
    [phone setTabBarItem:tab_item2];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:phone];

    CurrencyConvertorViewController *currency = [[CurrencyConvertorViewController alloc]init];
    UITabBarItem *tab_item3 = [[UITabBarItem alloc]init];
    tab_item3.title = @"Units Convertor";
    tab_item3.image = [UIImage imageNamed:@"Dollar.png"];
    [currency setTabBarItem:tab_item3];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:currency];

    SettingsPageViewController *setting = [[SettingsPageViewController alloc]init];
    UITabBarItem *tab_item4 = [[UITabBarItem alloc]init];
    tab_item4.title = @"Settings";
    tab_item4.image = [UIImage imageNamed:@"Settings.png"];
    [setting setTabBarItem:tab_item4];
    UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:setting];


    tabController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nav3, nav4, nil];
    self.window.rootViewController = tabController;

    [self.window makeKeyAndVisible];
    return YES;
}

I hope you got now

TamilKing
  • 1,643
  • 1
  • 12
  • 23
1

pushviewcontroller is the feature of UINavigationController where you can push one viewcontroller on another vierw controller. Here you are using a single viewcontroller as a rootviewcontroller so either you must change your rootviewcontroller to UINavigationcontroller or you can use "addSubview method" to add new viewController on the current viewcontroller.

but the better option is to add uinavigationcontroller as a rootviewcontroller.

AppDelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

     FirstViewController *first = [[FirstViewController alloc]
                                 initWithNibName:@"FirstViewController" bundle:nil];
     UINavigationController *navController =[[UINavigationController alloc] 
                                            initWithRootViewController:first];
     [self.window setRootViewController:navController];   

}

Now when as you want to switch from FirstViewController to SecondViewController on button clicked than you have to use pushviewcontroller

FirstViewController.h

-(void) nextBtnPressed {
    SecondViewController *second = [[SecondViewController alloc]  initWithNibName:@"SecondViewController" bundle:nil];
     [self.navigationController pushViewController:second animated:TRUE];
 }
kulss
  • 2,057
  • 1
  • 14
  • 16
  • When i wrote your code in `didFinishLaunchingWithOptions` there i am unable to see my Bottom `TabBar` – Krunal Oct 30 '13 at 07:09
  • 1. In Appdelegte.m you use to add navigation controller as rootviewcontroller(here FirstViewController is rootViewController of UINavigationController). 2. (when you are not using Storyboard)Now Suppose on next button pressed, you want switch from FirstViewController to SecondViewController than you must call nextBtnPressed method. – kulss Oct 30 '13 at 08:48