Switching Views Programmatically

Multi tool use
I have a few view controllers set up in IB. One that has a TableView that has about 6 cells (populated programmatically). And then 6 more view controllers (one for each cell).
I want to make it so that in my
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
I can make the view switch to whichever view is relative to the cell clicked.
So if they click cell1, it goes to view1, if they click cell2, it goes to view2.
This is how I init my table just btw and it works fine.
//tableview datasource delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return showArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.showLabel.text = [showArray objectAtIndex:indexPath.row];
cell.image.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
cell.seasonLabel.text = [seasonArray objectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 106;
}
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation{
NSIndexPath *newCellPath = [NSIndexPath indexPathForRow:showArray.count
inSection:0];
[self.theatreView insertRowsAtIndexPaths:@[newCellPath]
withRowAnimation:UITableViewRowAnimationFade];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
ios uiview tableview viewcontroller
add a comment |
I have a few view controllers set up in IB. One that has a TableView that has about 6 cells (populated programmatically). And then 6 more view controllers (one for each cell).
I want to make it so that in my
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
I can make the view switch to whichever view is relative to the cell clicked.
So if they click cell1, it goes to view1, if they click cell2, it goes to view2.
This is how I init my table just btw and it works fine.
//tableview datasource delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return showArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.showLabel.text = [showArray objectAtIndex:indexPath.row];
cell.image.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
cell.seasonLabel.text = [seasonArray objectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 106;
}
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation{
NSIndexPath *newCellPath = [NSIndexPath indexPathForRow:showArray.count
inSection:0];
[self.theatreView insertRowsAtIndexPaths:@[newCellPath]
withRowAnimation:UITableViewRowAnimationFade];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
ios uiview tableview viewcontroller
How have you got your app structured. Is it sitting in a navigation controller?
– Bergasms
Oct 29 '12 at 23:52
@Bergasms No it has one View controller that is the main menu, and it just goes from controller to controller using UIButtons
– tyler53
Oct 30 '12 at 0:02
add a comment |
I have a few view controllers set up in IB. One that has a TableView that has about 6 cells (populated programmatically). And then 6 more view controllers (one for each cell).
I want to make it so that in my
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
I can make the view switch to whichever view is relative to the cell clicked.
So if they click cell1, it goes to view1, if they click cell2, it goes to view2.
This is how I init my table just btw and it works fine.
//tableview datasource delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return showArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.showLabel.text = [showArray objectAtIndex:indexPath.row];
cell.image.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
cell.seasonLabel.text = [seasonArray objectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 106;
}
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation{
NSIndexPath *newCellPath = [NSIndexPath indexPathForRow:showArray.count
inSection:0];
[self.theatreView insertRowsAtIndexPaths:@[newCellPath]
withRowAnimation:UITableViewRowAnimationFade];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
ios uiview tableview viewcontroller
I have a few view controllers set up in IB. One that has a TableView that has about 6 cells (populated programmatically). And then 6 more view controllers (one for each cell).
I want to make it so that in my
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
I can make the view switch to whichever view is relative to the cell clicked.
So if they click cell1, it goes to view1, if they click cell2, it goes to view2.
This is how I init my table just btw and it works fine.
//tableview datasource delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return showArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil){
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.showLabel.text = [showArray objectAtIndex:indexPath.row];
cell.image.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
cell.seasonLabel.text = [seasonArray objectAtIndex:indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 106;
}
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation{
NSIndexPath *newCellPath = [NSIndexPath indexPathForRow:showArray.count
inSection:0];
[self.theatreView insertRowsAtIndexPaths:@[newCellPath]
withRowAnimation:UITableViewRowAnimationFade];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
ios uiview tableview viewcontroller
ios uiview tableview viewcontroller
edited Dec 28 '18 at 7:26
Cœur
17.5k9103145
17.5k9103145
asked Oct 29 '12 at 23:46
tyler53tyler53
3141415
3141415
How have you got your app structured. Is it sitting in a navigation controller?
– Bergasms
Oct 29 '12 at 23:52
@Bergasms No it has one View controller that is the main menu, and it just goes from controller to controller using UIButtons
– tyler53
Oct 30 '12 at 0:02
add a comment |
How have you got your app structured. Is it sitting in a navigation controller?
– Bergasms
Oct 29 '12 at 23:52
@Bergasms No it has one View controller that is the main menu, and it just goes from controller to controller using UIButtons
– tyler53
Oct 30 '12 at 0:02
How have you got your app structured. Is it sitting in a navigation controller?
– Bergasms
Oct 29 '12 at 23:52
How have you got your app structured. Is it sitting in a navigation controller?
– Bergasms
Oct 29 '12 at 23:52
@Bergasms No it has one View controller that is the main menu, and it just goes from controller to controller using UIButtons
– tyler53
Oct 30 '12 at 0:02
@Bergasms No it has one View controller that is the main menu, and it just goes from controller to controller using UIButtons
– tyler53
Oct 30 '12 at 0:02
add a comment |
3 Answers
3
active
oldest
votes
I would suggest one of these two methods based on how your app is structured.
[self.navigationController pushViewController:newViewController animated:YES];
[self presentModalViewController:newViewController animated:YES];
//for ios 6.0
[self presentViewController:newViewController animated:animated completion:NULL];
from what i can gather, you have a lot of uiviewcontrollers created in the .xib file of your main menu. is that correct? So you will have self.theatreViewController. You can then pass that as the parameter in the button callback. eg
[self presentViewController:self.theatreViewController animated:animated completion:NULL];
correct me if I am wrong in my assumptions here.
presentModalViewController:animated: deprecated in iOS 6 apparantly.. And also getting warning Incompatible pointer types sending 'TheatreViewController *_strong' to parameter of type 'UIViewController What I wrote was [self presentModalViewController: TheatreViewController animated:YES];
– tyler53
Oct 30 '12 at 0:06
you need to pass the actual variable to presentModalViewController. eg TheatreViewController *myviewController, you would pass myviewController in. Also good catch with the ios6 deprecate, i forgot about that. Instead you use [self presentViewController:modalViewController animated:animated completion:NULL];
– Bergasms
Oct 30 '12 at 1:05
that would work (your edit to your answer) but theatreViewController is not a property of Viewcontroller so I can't use self.theatreViewController But yes you are right I have many view controllers I just want to switch between them. Edit: I get this error when clicking the button now. 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ViewController: 0x7573de0>.
– tyler53
Oct 30 '12 at 2:48
I think the issue here is that for some reason it sees the viewcontroller that I am adding as nil, and I'm not sure why
– tyler53
Oct 30 '12 at 2:56
when you push a viewController check whether it is not nil and then push. if it is nil, alloc that viewController
– Bala
Oct 30 '12 at 5:45
|
show 1 more comment
I don't know the structure of your app, but I think you may consider this:
- (UIViewController *)getViewControllerAtIndexPath:(NSIndexPath *)indexPath {
//..you can use "switch" to return the view controller you want
return ...;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *controller = [self getViewControllerAtIndexPath:indexPath];
//..push or present your controller
}
add a comment |
Use the index path property in this method:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
like
yourObject = [yourArray objectAtIndex:indexPath];
EDIT: - In your AppDelegate in the following method, do this:-
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
mHomeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mHomeViewController];
[mHomeViewController release];
self.window.rootViewController = navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[navController release];
return YES;
}
Now, In your viewController - didSelectRowMethod, do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * object = [yourArray objectAtIndex:indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
if(newControllerInstance == nil) // check if it's nil, then initialize and alloc it.
{
newControllerInstance = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
[self.navigationController newControllerInstance animated:YES];
[newControllerInstance release];
}
}
This should work.
I know how to find the index, if you read the below comments it seems that my view controller is Nil for some reason. I don't know why though
– tyler53
Oct 30 '12 at 4:26
If you just want to push the view, use [self.navigationController pushViewController:ViewController animated:YES]; , do you initialize and allocate your view controller?
– Angad Manchanda
Oct 30 '12 at 4:31
I don't have a navigation controller, and what do you mean by initialze it and allocate it. you mean do I have an -(id) init and stuff?
– tyler53
Oct 30 '12 at 22:20
If you want to push to a view controller, you should have a navigaiton controller as the parent and you can use self.navigationController pushViewController:ViewController in order to push to a different view or you can use CATransitions (animations).
– Angad Manchanda
Oct 30 '12 at 23:05
How would i initialize a nav controller @Angad
– tyler53
Oct 31 '12 at 3:51
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13131283%2fswitching-views-programmatically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would suggest one of these two methods based on how your app is structured.
[self.navigationController pushViewController:newViewController animated:YES];
[self presentModalViewController:newViewController animated:YES];
//for ios 6.0
[self presentViewController:newViewController animated:animated completion:NULL];
from what i can gather, you have a lot of uiviewcontrollers created in the .xib file of your main menu. is that correct? So you will have self.theatreViewController. You can then pass that as the parameter in the button callback. eg
[self presentViewController:self.theatreViewController animated:animated completion:NULL];
correct me if I am wrong in my assumptions here.
presentModalViewController:animated: deprecated in iOS 6 apparantly.. And also getting warning Incompatible pointer types sending 'TheatreViewController *_strong' to parameter of type 'UIViewController What I wrote was [self presentModalViewController: TheatreViewController animated:YES];
– tyler53
Oct 30 '12 at 0:06
you need to pass the actual variable to presentModalViewController. eg TheatreViewController *myviewController, you would pass myviewController in. Also good catch with the ios6 deprecate, i forgot about that. Instead you use [self presentViewController:modalViewController animated:animated completion:NULL];
– Bergasms
Oct 30 '12 at 1:05
that would work (your edit to your answer) but theatreViewController is not a property of Viewcontroller so I can't use self.theatreViewController But yes you are right I have many view controllers I just want to switch between them. Edit: I get this error when clicking the button now. 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ViewController: 0x7573de0>.
– tyler53
Oct 30 '12 at 2:48
I think the issue here is that for some reason it sees the viewcontroller that I am adding as nil, and I'm not sure why
– tyler53
Oct 30 '12 at 2:56
when you push a viewController check whether it is not nil and then push. if it is nil, alloc that viewController
– Bala
Oct 30 '12 at 5:45
|
show 1 more comment
I would suggest one of these two methods based on how your app is structured.
[self.navigationController pushViewController:newViewController animated:YES];
[self presentModalViewController:newViewController animated:YES];
//for ios 6.0
[self presentViewController:newViewController animated:animated completion:NULL];
from what i can gather, you have a lot of uiviewcontrollers created in the .xib file of your main menu. is that correct? So you will have self.theatreViewController. You can then pass that as the parameter in the button callback. eg
[self presentViewController:self.theatreViewController animated:animated completion:NULL];
correct me if I am wrong in my assumptions here.
presentModalViewController:animated: deprecated in iOS 6 apparantly.. And also getting warning Incompatible pointer types sending 'TheatreViewController *_strong' to parameter of type 'UIViewController What I wrote was [self presentModalViewController: TheatreViewController animated:YES];
– tyler53
Oct 30 '12 at 0:06
you need to pass the actual variable to presentModalViewController. eg TheatreViewController *myviewController, you would pass myviewController in. Also good catch with the ios6 deprecate, i forgot about that. Instead you use [self presentViewController:modalViewController animated:animated completion:NULL];
– Bergasms
Oct 30 '12 at 1:05
that would work (your edit to your answer) but theatreViewController is not a property of Viewcontroller so I can't use self.theatreViewController But yes you are right I have many view controllers I just want to switch between them. Edit: I get this error when clicking the button now. 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ViewController: 0x7573de0>.
– tyler53
Oct 30 '12 at 2:48
I think the issue here is that for some reason it sees the viewcontroller that I am adding as nil, and I'm not sure why
– tyler53
Oct 30 '12 at 2:56
when you push a viewController check whether it is not nil and then push. if it is nil, alloc that viewController
– Bala
Oct 30 '12 at 5:45
|
show 1 more comment
I would suggest one of these two methods based on how your app is structured.
[self.navigationController pushViewController:newViewController animated:YES];
[self presentModalViewController:newViewController animated:YES];
//for ios 6.0
[self presentViewController:newViewController animated:animated completion:NULL];
from what i can gather, you have a lot of uiviewcontrollers created in the .xib file of your main menu. is that correct? So you will have self.theatreViewController. You can then pass that as the parameter in the button callback. eg
[self presentViewController:self.theatreViewController animated:animated completion:NULL];
correct me if I am wrong in my assumptions here.
I would suggest one of these two methods based on how your app is structured.
[self.navigationController pushViewController:newViewController animated:YES];
[self presentModalViewController:newViewController animated:YES];
//for ios 6.0
[self presentViewController:newViewController animated:animated completion:NULL];
from what i can gather, you have a lot of uiviewcontrollers created in the .xib file of your main menu. is that correct? So you will have self.theatreViewController. You can then pass that as the parameter in the button callback. eg
[self presentViewController:self.theatreViewController animated:animated completion:NULL];
correct me if I am wrong in my assumptions here.
edited Oct 30 '12 at 1:05
answered Oct 29 '12 at 23:54


BergasmsBergasms
1,91311216
1,91311216
presentModalViewController:animated: deprecated in iOS 6 apparantly.. And also getting warning Incompatible pointer types sending 'TheatreViewController *_strong' to parameter of type 'UIViewController What I wrote was [self presentModalViewController: TheatreViewController animated:YES];
– tyler53
Oct 30 '12 at 0:06
you need to pass the actual variable to presentModalViewController. eg TheatreViewController *myviewController, you would pass myviewController in. Also good catch with the ios6 deprecate, i forgot about that. Instead you use [self presentViewController:modalViewController animated:animated completion:NULL];
– Bergasms
Oct 30 '12 at 1:05
that would work (your edit to your answer) but theatreViewController is not a property of Viewcontroller so I can't use self.theatreViewController But yes you are right I have many view controllers I just want to switch between them. Edit: I get this error when clicking the button now. 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ViewController: 0x7573de0>.
– tyler53
Oct 30 '12 at 2:48
I think the issue here is that for some reason it sees the viewcontroller that I am adding as nil, and I'm not sure why
– tyler53
Oct 30 '12 at 2:56
when you push a viewController check whether it is not nil and then push. if it is nil, alloc that viewController
– Bala
Oct 30 '12 at 5:45
|
show 1 more comment
presentModalViewController:animated: deprecated in iOS 6 apparantly.. And also getting warning Incompatible pointer types sending 'TheatreViewController *_strong' to parameter of type 'UIViewController What I wrote was [self presentModalViewController: TheatreViewController animated:YES];
– tyler53
Oct 30 '12 at 0:06
you need to pass the actual variable to presentModalViewController. eg TheatreViewController *myviewController, you would pass myviewController in. Also good catch with the ios6 deprecate, i forgot about that. Instead you use [self presentViewController:modalViewController animated:animated completion:NULL];
– Bergasms
Oct 30 '12 at 1:05
that would work (your edit to your answer) but theatreViewController is not a property of Viewcontroller so I can't use self.theatreViewController But yes you are right I have many view controllers I just want to switch between them. Edit: I get this error when clicking the button now. 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ViewController: 0x7573de0>.
– tyler53
Oct 30 '12 at 2:48
I think the issue here is that for some reason it sees the viewcontroller that I am adding as nil, and I'm not sure why
– tyler53
Oct 30 '12 at 2:56
when you push a viewController check whether it is not nil and then push. if it is nil, alloc that viewController
– Bala
Oct 30 '12 at 5:45
presentModalViewController:animated: deprecated in iOS 6 apparantly.. And also getting warning Incompatible pointer types sending 'TheatreViewController *_strong' to parameter of type 'UIViewController What I wrote was [self presentModalViewController: TheatreViewController animated:YES];
– tyler53
Oct 30 '12 at 0:06
presentModalViewController:animated: deprecated in iOS 6 apparantly.. And also getting warning Incompatible pointer types sending 'TheatreViewController *_strong' to parameter of type 'UIViewController What I wrote was [self presentModalViewController: TheatreViewController animated:YES];
– tyler53
Oct 30 '12 at 0:06
you need to pass the actual variable to presentModalViewController. eg TheatreViewController *myviewController, you would pass myviewController in. Also good catch with the ios6 deprecate, i forgot about that. Instead you use [self presentViewController:modalViewController animated:animated completion:NULL];
– Bergasms
Oct 30 '12 at 1:05
you need to pass the actual variable to presentModalViewController. eg TheatreViewController *myviewController, you would pass myviewController in. Also good catch with the ios6 deprecate, i forgot about that. Instead you use [self presentViewController:modalViewController animated:animated completion:NULL];
– Bergasms
Oct 30 '12 at 1:05
that would work (your edit to your answer) but theatreViewController is not a property of Viewcontroller so I can't use self.theatreViewController But yes you are right I have many view controllers I just want to switch between them. Edit: I get this error when clicking the button now. 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ViewController: 0x7573de0>.
– tyler53
Oct 30 '12 at 2:48
that would work (your edit to your answer) but theatreViewController is not a property of Viewcontroller so I can't use self.theatreViewController But yes you are right I have many view controllers I just want to switch between them. Edit: I get this error when clicking the button now. 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ViewController: 0x7573de0>.
– tyler53
Oct 30 '12 at 2:48
I think the issue here is that for some reason it sees the viewcontroller that I am adding as nil, and I'm not sure why
– tyler53
Oct 30 '12 at 2:56
I think the issue here is that for some reason it sees the viewcontroller that I am adding as nil, and I'm not sure why
– tyler53
Oct 30 '12 at 2:56
when you push a viewController check whether it is not nil and then push. if it is nil, alloc that viewController
– Bala
Oct 30 '12 at 5:45
when you push a viewController check whether it is not nil and then push. if it is nil, alloc that viewController
– Bala
Oct 30 '12 at 5:45
|
show 1 more comment
I don't know the structure of your app, but I think you may consider this:
- (UIViewController *)getViewControllerAtIndexPath:(NSIndexPath *)indexPath {
//..you can use "switch" to return the view controller you want
return ...;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *controller = [self getViewControllerAtIndexPath:indexPath];
//..push or present your controller
}
add a comment |
I don't know the structure of your app, but I think you may consider this:
- (UIViewController *)getViewControllerAtIndexPath:(NSIndexPath *)indexPath {
//..you can use "switch" to return the view controller you want
return ...;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *controller = [self getViewControllerAtIndexPath:indexPath];
//..push or present your controller
}
add a comment |
I don't know the structure of your app, but I think you may consider this:
- (UIViewController *)getViewControllerAtIndexPath:(NSIndexPath *)indexPath {
//..you can use "switch" to return the view controller you want
return ...;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *controller = [self getViewControllerAtIndexPath:indexPath];
//..push or present your controller
}
I don't know the structure of your app, but I think you may consider this:
- (UIViewController *)getViewControllerAtIndexPath:(NSIndexPath *)indexPath {
//..you can use "switch" to return the view controller you want
return ...;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *controller = [self getViewControllerAtIndexPath:indexPath];
//..push or present your controller
}
answered Oct 30 '12 at 4:00
OscarOscar
631513
631513
add a comment |
add a comment |
Use the index path property in this method:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
like
yourObject = [yourArray objectAtIndex:indexPath];
EDIT: - In your AppDelegate in the following method, do this:-
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
mHomeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mHomeViewController];
[mHomeViewController release];
self.window.rootViewController = navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[navController release];
return YES;
}
Now, In your viewController - didSelectRowMethod, do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * object = [yourArray objectAtIndex:indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
if(newControllerInstance == nil) // check if it's nil, then initialize and alloc it.
{
newControllerInstance = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
[self.navigationController newControllerInstance animated:YES];
[newControllerInstance release];
}
}
This should work.
I know how to find the index, if you read the below comments it seems that my view controller is Nil for some reason. I don't know why though
– tyler53
Oct 30 '12 at 4:26
If you just want to push the view, use [self.navigationController pushViewController:ViewController animated:YES]; , do you initialize and allocate your view controller?
– Angad Manchanda
Oct 30 '12 at 4:31
I don't have a navigation controller, and what do you mean by initialze it and allocate it. you mean do I have an -(id) init and stuff?
– tyler53
Oct 30 '12 at 22:20
If you want to push to a view controller, you should have a navigaiton controller as the parent and you can use self.navigationController pushViewController:ViewController in order to push to a different view or you can use CATransitions (animations).
– Angad Manchanda
Oct 30 '12 at 23:05
How would i initialize a nav controller @Angad
– tyler53
Oct 31 '12 at 3:51
|
show 1 more comment
Use the index path property in this method:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
like
yourObject = [yourArray objectAtIndex:indexPath];
EDIT: - In your AppDelegate in the following method, do this:-
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
mHomeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mHomeViewController];
[mHomeViewController release];
self.window.rootViewController = navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[navController release];
return YES;
}
Now, In your viewController - didSelectRowMethod, do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * object = [yourArray objectAtIndex:indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
if(newControllerInstance == nil) // check if it's nil, then initialize and alloc it.
{
newControllerInstance = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
[self.navigationController newControllerInstance animated:YES];
[newControllerInstance release];
}
}
This should work.
I know how to find the index, if you read the below comments it seems that my view controller is Nil for some reason. I don't know why though
– tyler53
Oct 30 '12 at 4:26
If you just want to push the view, use [self.navigationController pushViewController:ViewController animated:YES]; , do you initialize and allocate your view controller?
– Angad Manchanda
Oct 30 '12 at 4:31
I don't have a navigation controller, and what do you mean by initialze it and allocate it. you mean do I have an -(id) init and stuff?
– tyler53
Oct 30 '12 at 22:20
If you want to push to a view controller, you should have a navigaiton controller as the parent and you can use self.navigationController pushViewController:ViewController in order to push to a different view or you can use CATransitions (animations).
– Angad Manchanda
Oct 30 '12 at 23:05
How would i initialize a nav controller @Angad
– tyler53
Oct 31 '12 at 3:51
|
show 1 more comment
Use the index path property in this method:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
like
yourObject = [yourArray objectAtIndex:indexPath];
EDIT: - In your AppDelegate in the following method, do this:-
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
mHomeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mHomeViewController];
[mHomeViewController release];
self.window.rootViewController = navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[navController release];
return YES;
}
Now, In your viewController - didSelectRowMethod, do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * object = [yourArray objectAtIndex:indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
if(newControllerInstance == nil) // check if it's nil, then initialize and alloc it.
{
newControllerInstance = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
[self.navigationController newControllerInstance animated:YES];
[newControllerInstance release];
}
}
This should work.
Use the index path property in this method:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
like
yourObject = [yourArray objectAtIndex:indexPath];
EDIT: - In your AppDelegate in the following method, do this:-
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
mHomeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mHomeViewController];
[mHomeViewController release];
self.window.rootViewController = navController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[navController release];
return YES;
}
Now, In your viewController - didSelectRowMethod, do this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * object = [yourArray objectAtIndex:indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
if(newControllerInstance == nil) // check if it's nil, then initialize and alloc it.
{
newControllerInstance = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
[self.navigationController newControllerInstance animated:YES];
[newControllerInstance release];
}
}
This should work.
edited Oct 30 '12 at 23:15
answered Oct 30 '12 at 4:18
Angad ManchandaAngad Manchanda
108517
108517
I know how to find the index, if you read the below comments it seems that my view controller is Nil for some reason. I don't know why though
– tyler53
Oct 30 '12 at 4:26
If you just want to push the view, use [self.navigationController pushViewController:ViewController animated:YES]; , do you initialize and allocate your view controller?
– Angad Manchanda
Oct 30 '12 at 4:31
I don't have a navigation controller, and what do you mean by initialze it and allocate it. you mean do I have an -(id) init and stuff?
– tyler53
Oct 30 '12 at 22:20
If you want to push to a view controller, you should have a navigaiton controller as the parent and you can use self.navigationController pushViewController:ViewController in order to push to a different view or you can use CATransitions (animations).
– Angad Manchanda
Oct 30 '12 at 23:05
How would i initialize a nav controller @Angad
– tyler53
Oct 31 '12 at 3:51
|
show 1 more comment
I know how to find the index, if you read the below comments it seems that my view controller is Nil for some reason. I don't know why though
– tyler53
Oct 30 '12 at 4:26
If you just want to push the view, use [self.navigationController pushViewController:ViewController animated:YES]; , do you initialize and allocate your view controller?
– Angad Manchanda
Oct 30 '12 at 4:31
I don't have a navigation controller, and what do you mean by initialze it and allocate it. you mean do I have an -(id) init and stuff?
– tyler53
Oct 30 '12 at 22:20
If you want to push to a view controller, you should have a navigaiton controller as the parent and you can use self.navigationController pushViewController:ViewController in order to push to a different view or you can use CATransitions (animations).
– Angad Manchanda
Oct 30 '12 at 23:05
How would i initialize a nav controller @Angad
– tyler53
Oct 31 '12 at 3:51
I know how to find the index, if you read the below comments it seems that my view controller is Nil for some reason. I don't know why though
– tyler53
Oct 30 '12 at 4:26
I know how to find the index, if you read the below comments it seems that my view controller is Nil for some reason. I don't know why though
– tyler53
Oct 30 '12 at 4:26
If you just want to push the view, use [self.navigationController pushViewController:ViewController animated:YES]; , do you initialize and allocate your view controller?
– Angad Manchanda
Oct 30 '12 at 4:31
If you just want to push the view, use [self.navigationController pushViewController:ViewController animated:YES]; , do you initialize and allocate your view controller?
– Angad Manchanda
Oct 30 '12 at 4:31
I don't have a navigation controller, and what do you mean by initialze it and allocate it. you mean do I have an -(id) init and stuff?
– tyler53
Oct 30 '12 at 22:20
I don't have a navigation controller, and what do you mean by initialze it and allocate it. you mean do I have an -(id) init and stuff?
– tyler53
Oct 30 '12 at 22:20
If you want to push to a view controller, you should have a navigaiton controller as the parent and you can use self.navigationController pushViewController:ViewController in order to push to a different view or you can use CATransitions (animations).
– Angad Manchanda
Oct 30 '12 at 23:05
If you want to push to a view controller, you should have a navigaiton controller as the parent and you can use self.navigationController pushViewController:ViewController in order to push to a different view or you can use CATransitions (animations).
– Angad Manchanda
Oct 30 '12 at 23:05
How would i initialize a nav controller @Angad
– tyler53
Oct 31 '12 at 3:51
How would i initialize a nav controller @Angad
– tyler53
Oct 31 '12 at 3:51
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13131283%2fswitching-views-programmatically%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Eri2ygl9Js42w 5VEZK0q2x HxqgGuFVK84
How have you got your app structured. Is it sitting in a navigation controller?
– Bergasms
Oct 29 '12 at 23:52
@Bergasms No it has one View controller that is the main menu, and it just goes from controller to controller using UIButtons
– tyler53
Oct 30 '12 at 0:02