I have a custom class which is a UITableViewController, this is inside of a UINavigationController. Normally, when I click a cell, I push a new class onto the stack and it is fine. This time, I would like to push self onto the stack (passing a different string onLoad so that I load different content), so that I can reuse my code, is this possible? Or do I always have to create a secondary class to push?
Rather than "pushing yourself", you can push a new instance of the same view controller yes. Simply create a new one like so:
MyViewController *viewController = [[MyViewController alloc] initWithNibName:"MyView"];
viewController.customString = #"Something else";
[self.navigationController pushViewController:viewController];
[viewController release];
I haven't tested this, and it's late so there might be bits wrong but you should be ok with that. Let me know if it works!
My solution:
MyViewController *viewController = [[MyViewController alloc] initWithNibName:"MyView"];
viewController.customString = #"Something else";
[self.navigationController pushViewController:viewController];
Related
I have a UIViewController class and a second which I want to push modally preferably. However I can't seem to call [self pushModalViewController:...], how come?
What requirements do I need to meet to be able to do so?
I am doing this and getting a black view pushed:
vc = [[ViewController alloc] init];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vc animated:YES];
I have made my view controller in my storyboard and given it a custom class. I am trying to present this view modally via this class as seen in my code.
Any help much appreciated, thanks.
'push' and 'modal' don't belong together in the same thought. You can:
present a modal view controller, preferably using -presentViewController:animated:completion:, which is the modern replacement for -presentModalViewController:animated:
push a view controller onto the navigation stack, assuming that you're using a UINavigationController. To do that from a view controller, use:
[self.navigationController pushViewController:foo animated:YES];
You're actually looking for [self presentModalViewController:myViewController.view animated:YES]
How are you initing this viewcontroller. From the posted code, I assume it is being initialized to a blank view.
Maybe you can init it from a nibname or something?
vc = [[ViewController alloc] initWithNibName:#"NibName" bundle:nil];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vc animated:YES];
Also another thing to note, if this is a custom class and no nib file, is there any code in initWithCoder? How does the viewDidLoad looks like?
I am using this code to hide a button in a different view controller, but the button does not get hidden when the button is pressed to hide the button in the other view controller.
This is the code I am using to hide the button in the other view controller:
[self dismissModalViewControllerAnimated:YES];
NSLog(#"Exited");
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[vc.mainbutton1 setHidden:YES];
Why is this not working?
Thanks!
take a BOOL variable in ViewController controller and make the property and synthesize also.
and do this.
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
vc.check = YES;
in the view controller viewdidload
write this
if(self.check)
[mainbutton1 set hidden:YES];
The other answers should work unless...
Judging by your code I am going to guess that you are trying to hide a button on the viewController that presented the modal view?
If this is correct then what you are doing will not work as you are creating a new instance of ViewController which is not the already existing viewController you want to use.
Although the docs say that it is fine to call [self dismissModalViewControllerAnimated:YES]; from the presented modal view I tend to set up a delegate to handle the dismissal like in Apple's utitliy app template.
The reason this isn't working is because even though you have alloc'd and init'd the ViewController properly, the actual elements of that vc ViewController (including mainbutton1) have not been loaded yet.
Hitman has the right idea (and I'm voting his idea up).
Either put in a BOOL property for setting mainButton1 to hidden when the view appears, or call your [mainButton1 setHidden: YES] right after you explicitly display the view (via animation or adding subviews or whatever).
From your question it sounds like you want to hide the button in an existing view controller, whereas in your code you are creating a new one
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[vc.mainbutton1 setHidden:YES];
Either the view controller which you observe is not the one you expect or the mainbutton1 outlet is not connected properly. You can check if the memory controller is the one you expect by logging its memory address.
NSLog(#"Hid button for view controller %p", vc);
And doing the same in the viewDidAppear callback of ViewController
NSLog(#"In viewDidAppear for view controller %p", self);
It seems you want a certain button to be hidden if something has been happening somewhere else.
You COULD, somewhat as a hack (but I don't mind that very much) control this with a variable on your AppDelegate for instance.
When the "something" is happening "somewhere else", do this:
MyAppDelegate *appDelegate = [[(MyAppDelegate *)UIApplication sharedApplication] delegate];
appDelegate.shouldHideThatOtherButtonLater = YES;
Then, when you create your new ViewController later on you could use this value to determine if your button should be visible or not like this:
ViewController *vc = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
MyAppDelegate *appDelegate = [[(MyAppDelegate *)UIApplication sharedApplication] delegate];
[vc.mainbutton1 setHidden: appDelegate.shouldHideThatOtherButtonLater ];
You will in this case have to prepare your AppDelegate for this by creating and synthesizing that shouldHideThatOtherButtonLater-property.
I have the following Objective-C code:
MainMenu *main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:main animated:YES];
NSLog(#"hello");
I have a class called 'MainMenu' with a corresponding header file and xib file. No matter what I do, it simply won't show. I have confirmed that the code gets to the above, because of the NSLog('hello').
I've been pulling my hair out for hours now and I simply cannot get to the bottom of it.
I hope someone can help,
Edit - still having problems...
Here are some screenshots of my project setup:
Ok, so I tried this:
MainMenu *main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[[self navigationController]pushViewController:main animated:YES];
[self.view addSubview:main.view];
But it still doesn't work...
Many thanks in advance,
The fact that initWithNibName is nil should not be the problem because if it is given nil it looks for a nib with the exact name of the class.
Two things:
1) Make sure you have run a clean recently and make sure that file is correctly being loaded.
2) Make sure navigationController is not nil, if it is then you need to make sure you make a navigation controller if you are not intending on using a navigation controller, consider using:
- (void)presentModalViewController:(UIViewController *)modalViewController
animated:(BOOL)animated
Why are you setting the nibName to nil? If the name of the nib is MainMenu, then you want:
MainMenu *main= [[MainMenu alloc] initWithNibName:#"MainMenu" bundle:nil];
[self.navigationController pushViewController:main animated:YES];
[main release];
Are you sure that you have a UINavigationController in order to push a new view?
Hope that Helps!
Dont pull your hair just look at your code closely: You have nil in your initWithNibName. Whats MainMenu is it a viewController or what ? and place your correct nib for your to get Hello.
Updated as asked :
MainMenu *main= (MainMenu *)[MainMenu alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main];
[self.navigationController presentModalViewController:nav animated:YES];
NSLog(#"hello");
I guess the issue is that you do not have a navigation controller set up .Try to present the view controller by
[self presentModalViewController:main animated:YES];
Verify that the object owner is in fact MainMenu and the view is connected.
In the MainMenu NIB, select File's Owner and the click on the Identity Inspector. Class should match your VC class name.
Then select the main View in your NIB and click on the Connections Inspector. The view outlet should be connected to your File's Owner.
If those are both set correctly, then post some more surrounding code. Notable point, if those are both set,
MainMenu * main= [[MainMenu alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:main animated:YES];
will load the correct NIB and display it.
What are you seeing? Another point if MainMenu is a subclass of some other VC with a NIB you will have to change the base class' init to override the default behavior, for example:
self = [super initWithNibName:nibNameOrNil == nil ? #"BaseViewController" : nibNameOrNil bundle:nibBundleOrNil]
But in that case you would have to specific the NIB that will override the base view controller's.
Post more code and let us know what you are seeing when you run.
I have a UIViewController that I wish to push onto the stack, however, when I call [viewController release] on it after I push it, any time I pop it off of the stack however, I get various errors pertaining to deallocated instances of the view controller. Here is an example of an implementation:
RootViewController *rootViewcontroller = [[RootViewController alloc] initWithNibName:#"RootViewController"
bundle:nil];
[self.navigationController pushViewController:rootViewController animated:YES];
[rootViewController release];
In the code that you showed you create rootViewController but actually push personsViewController. Typo? Or did you paste the wrong code?
Your RootViewController instance is "rootViewcontroller" but you're pushing "rootViewController" (notice the 'C'?).
view1 = [[View1 alloc] init]; //Create the first view
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];
navigationController1.navigationBar.tintColor =[UIColor blackColor];
View1 is inherit from UIViewController. So I create a *view1, then I create a UINavigationController, call *navigationController1. How do I link the two together? Thank you very much
The way to link a view controller with a navigation controller is to push the view controller onto the navigation stack. For example:
UIViewController * yourViewController = [[UIViewController alloc] init];
UINavigationController * navigation = [[UINavigationController alloc] init];
[navigation pushViewController:yourViewController animated:NO];
[yourViewController release]
Finally release the view controller at the end since the navigation controller retains it.
You may have things a little mixed up. A UINavigationController is generally attached to a UIViewController, which itself is what contains the UIView.
Before writing your own code, you might take a look at the navigation controller sample application project that is available from Xcode's new project template list, to figure out how it works.
The answer for this question is here: Having problem with pushViewController!! Help