-1

I´m new to iOS development and am facing what to me seems to be a trivial task: open view controller 2 when pushing a button in view controller1. No storyboard and xib files involved, so the methods I found by web search like initWithNibName, usage of segue e.a. are not applicable.

Update:

Still struggling with this issue. When switching to ViewController2 by pushing a button in ViewController1 a blank (white) view opens in the app. I meanwhile switched to using a storyboard as I need navigation control. But still no xib files in use.

Here the relevant code from ViewController1.m:

#import "ViewController2.h"

@interface ViewController1 ()

@property (strong, nonatomic) IBOutlet UIButton *GoToViewController2;

@end

@implementation ViewController1

- (IBAction)GoToViewController2:(id)sender
{
    ViewController2* vc2 = [[ViewController2 alloc] init];
    [self.navigationController pushViewController:vc2 animated:YES];
    NSLog(@"Went to ViewController2");
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
groszmann
  • 1
  • 2
  • use this link this is help with u http://stackoverflow.com/questions/20742745/navigation-controller-push-view-controller/20742996#20742996 – Anbu.Karthik Aug 18 '14 at 11:02

2 Answers2

1

In the method for your button;

SomeViewController *someVC = [[SomeViewController alloc] init];
[self.navigationController pushViewController:someVC animated:YES];

That'll push the new view controller on the default navigationController stack and animate the transition.

sreekanthk
  • 362
  • 2
  • 21
Nicholas Smith
  • 11,642
  • 6
  • 37
  • 55
  • and if you don't have a navigationController? – Bogdan Somlea Aug 18 '14 at 12:50
  • Then use `[self presentViewController: animated: completion: ];`, but for most peoples scenarios then navigation controller is the best way to go to manage the view controller stack. Especially as the asker said they were new to iOS development. – Nicholas Smith Aug 18 '14 at 14:11
  • if there is no storyboard, and he is asking this, i dont' really think he have the navigationController – Bogdan Somlea Aug 18 '14 at 18:33
0

To present modally a new view controller over the existing one, you have to alloc & init your viewController2 and then present it from the viewController1:

VC2 *vc2 = [[VC2 alloc] init];
//Do initialization ViewController2 stuff here, if needed
....................
//Then present it from your ViewController1
[self presentViewController:vc2 animated:YES completion:nil];
lihudi
  • 860
  • 1
  • 12
  • 21
  • Thanks for quick reply. Tried to test that in a freshly setup test project and here getting error: _no visible @interface for 'GRViewController' declares the selector 'presentViewController:animated:''_ with this code in GRViewController.m: - (IBAction)GoToViewController2:(id)sender { GRViewController2 *grVC2 = [[GRViewController2 alloc] init]; [self presentViewController:grVC2 animated:YES]; } – groszmann Aug 19 '14 at 08:40
  • Sorry, looks like I forgot something: with code completed _- (IBAction)GoToViewController2:(id)sender { GRViewController2 *grVC2 = [[GRViewController2 alloc] init]; [self presentViewController:grVC2 animated:YES **completion:nil**]; }_ the error disappears. – groszmann Aug 19 '14 at 08:51