Reputation: 31
before asking this question, I did some research of course. One of the links or topics I have found on google and on stackoverflow is this:
How do I load another view controller from the current view controller's implementation file?
In my case, I have two viewcontrollers in my project, namely:
ViewController (ViewController.h and ViewController.m) - my main vc
LeftViewController (LeftViewController.h and LeftViewController.m) - my second vc. It will be loaded upon clicking a button from my main vc.
Please take note that it is my 3rd day in my job, self training, and I find objective-c quite hard compared to others, like C# ... Oh I miss C#.
Going on, I tried following the steps from the links that I've got as well as the link I gave at the top.
Here are my codes:
a. Inside my ViewDidLoad in my ViewController.m
// add navigation button left
UIButton *leftNavigationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[leftNavigationButton setFrame:CGRectMake(10, 65, 30, 20)];
[leftNavigationButton setTitle:@"<" forState:UIControlStateNormal];
[leftNavigationButton addTarget:self action:@selector(navigateLeft) forControlEvents:UIControlEventTouchUpInside];
[leftNavigationButton.layer setBorderWidth:1.0f];
[leftNavigationButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[leftNavigationButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.view addSubview:leftNavigationButton];
b. the method inside my ViewController.m (of course outside the ViewDidLoad:
- (void)navigateLeft
{
// LeftViewController *leftViewController = [[LeftViewController alloc] init];
// [leftViewController viewDidLoad];
_leftViewController = [[LeftViewController alloc] initWithNibName:@"UI For LeftButton" bundle:nil];
[self.ViewController: self.leftViewController animated:YES completion:nil]; //Error in this line, spefically in self.ViewController<---
}
c. ViewController.h
//
// ViewController.h
// Navigation_Task
//
// Created by Glenn on 9/4/15.
// Copyright (c) 2015 Ayi. All rights reserved.
//
#import <UIKit/UIKit.h>
// for LeftViewController
@class LeftViewController;
@interface ViewController : UIViewController
@property (nonatomic, retain) NSMutableString *resultText;
// for LeftViewController
@property (strong, nonatomic)LeftViewController *leftViewController;
@end
and lastly, (this is not included in the link above), my code for loading the LeftViewController.
a. viewDidLoad in LeftViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
LeftViewController *leftViewController = [[LeftViewController alloc] initWithNibName:nil bundle:nil];
// set background and title of the view
[leftViewController.view setBackgroundColor:RGB(19,181,234)];
leftViewController.title = @"Left View Controller";
[self.navigationController pushViewController:leftViewController animated:YES];
}
Sorry if this question is too long, I can't shoot my questions to the developers here in the office, because I'm shy and they don't wanna be bothered :(
Upvotes: 0
Views: 474
Reputation: 31
I would like to answer my own question.
I have two buttons in my main ViewController.
1 button for LeftViewController
1 button for RightViewController.
Meaning I need to display each new viewcontroller or screen when I click 1 of those buttons.
Here are my codes:
Just inside the event handler (is my term correct? I'm from windows) of the two buttons, put the following:
Code for 'Left Screen/View Controller'
- (void)navigateLeft
{
// LeftViewController *leftViewController = [[LeftViewController alloc] init];
// [leftViewController viewDidLoad];
// _leftViewController = [[LeftViewController alloc] initWithNibName:@"UI For LeftButton" bundle:nil];
LeftViewController *leftViewController = [[LeftViewController alloc] initWithNibName:nil bundle:nil];
// set background and title of the view
[leftViewController.view setBackgroundColor:RGB(19,181,234)];
leftViewController.title = @"Left View Controller";
[self.navigationController pushViewController:leftViewController animated:YES];}
Similarly, I need to put the same type of codes (just renaming the leftViewController into RightViewController)
- (void)navigateRight
{
RightViewController *rightViewController = [[RightViewController alloc] initWithNibName:nil bundle:nil];
// set background and title of the view
[rightViewController.view setBackgroundColor:RGB(19,181,234)];
rightViewController.title = @"Right View Controller";
[self.navigationController pushViewController:rightViewController animated:YES];
}
Now I can push any of the two button and will display a new ViewController (or screen).
Upvotes: 0
Reputation: 4645
I suggest you take a look into Apple's documentation on UINavigation.
You can either add as many viewController objects into the navigation stack as needed or present
it incase you need to show it temporarily.
Upvotes: 1
Reputation: 98
You are using right Controller for navigation i.e. UINavigationController, but to make it navigate I will recommend you to use storyboards. Take your UINavigationController as a rootviewcontroller and further take any UIViewController as its rootviewcontroller and then on 2nd UIViewController to navigate attach a Segue to it and provide it a identifier let say "PushToSecond" and in your buttons Method do the following:
[self performSegueWithIdentifier:@"PushToSecond" sender:self];
}
hope this would help
Upvotes: 1