In my app I've added a label to a view, connected it to an outlet but nothing shows up when I first assign this outlet from another view controller and then call pushViewController to display it. Here's the code before pushing next view that display the label:
CustomViewController *vc = [[CustomViewController alloc] init];
vc.lbl_price.text = self.label_price.text; // lbl_price is defined as a property in CustomViewController and label_price is defined in current view controller
[self.navigationController pushViewController:vc];
In the CustomViewController viewDidLoad method I added this instruction to see if it should work
NSLog(#"Price=%#",lbl_price); // it actually prints out what was previously assigned
But it doesn't show into the label!
Any idea why ?
Stephane
Even if view controller is created its view hierarchy may not (and so all subviews will still be nil), for optimization reasons it may not be loaded until you try to actually access controller's view. You have two options to solve your problem:
Store all values in separate non-UI variables and assign them to UI components with controller is going to appear:
// Before push controller
vc.myPriceText = self.label_price.text;
// In controller's viewWillAppear:
self.lbl_price.text = self.myPriceText;
Make [vc view] call to force controller to load its view hierarchy:
CustomViewController *vc = [[CustomViewController alloc] init];
[vc view];
vc.lbl_price.text = self.label_price.text;
[self.navigationController pushViewController:vc];
Related
I'm writting my first app using a storyboard. Before I had the following code to go from one view controller to another. But..this code needs a NibName. How would I do this going from screens created in a storyboard?
if (mDisplayCard==nil)
{
mDisplayCard = [[cDisplayCard alloc]
initWithNibName:#"cDisplayCard"
bundle:[NSBundle mainBundle]];
}
// [ mDisplay SetUp];
[self.navigationController pushViewController: mDisplayCard animated:YES];
A line like the following will instantiate a new view controller from a storyboard:
UIViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"<identifier-from-storyboard>"];
It is assumed that your current view controller was instantiated from the same storyboard, so that self.storyboard is not nil. For the view controller that you want to instantiate, you'll have to make sure that you set a storyboard ID for it, which is what you'll put in for the identifier.
I'm not sure why but in Xcode 5 working on a project of IOS6.1 I have a button connected to a IBAction in which I'm trying to navigate to a new view controller.
I've tried two different codes to create the viewController and then push it to the navigation in both cases the view controller is not nil and both cases the viewController doesn't appear.
first try: with story Id - I've set the story id of the view controller to imageCapture and set the class to VSImageCaptureViewController
VSImageCaptureViewController* imageCaptureViewController = (VSImageCaptureViewController*)([self.storyboard instantiateViewControllerWithIdentifier:#"imageCapture"]);
[self presentViewController:imageCaptureViewController animated:NO completion:nil];
second try: with the name of the viewcontroller
VSImageCaptureViewController *imageCaptureViewController = [[VSImageCaptureViewController alloc] initWithNibName:#"VSImageCaptureViewController" bundle:nil];
[self.navigationController pushViewController:imageCaptureViewController animated:YES];
can you see something wrong or do you think I forgot to initialize something
Check to see if self.navigationController is nil.
If it is nil that means that you are not running within the context of a UINavigationController (the system sets this property for you when the UIViewController is added to a nav stack).
If this is the case then you have not properly set up a UINavigationController.
Note that you can not set the navigationController property yourself. The systems sets it for you when the UIViewController is added to a UINavigationController's stack (and sets it to nil when it is removed from the stack).
To set this up you will usually create a UINavigationController instance right after you create your main view controller.
UIViewController *mainViewController = ...;
UINavigationController *mainNavController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
// now present the mainNavController instead of the mainViewController
If you are using storyboards you would drag out a UINavigationController instance and replace the default root view controller with an instance of your mainViewController.
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 a view that shows a map. I have a custom subclass of UIViewController (DetailViewController) that gets shown when the detailDisclosureButton of the callout above the pin is pressed. While in my map class, I create my detailview and add it to the subview like this:
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detailView.locationPoint = locationPoint;
detailView.locationCoordinate = locationCoordinate;
[self.view addSubview:detailView.view];
[detailView release];
My DetailViewController has a TableView and parses the data in DetailViewController. However I get an error of sending the numberOfSectionsInTable message to a dealloc'd instance. I'm assuming it is this since I originally had this as a property and it worked fine with (nonatomic, retain). I'm assuming that I'm releasing it before the next view is done with it. If that is the case, when would I clean up the memory??? It seems like this would be the place to do it. Thanks.
I am not sure what makes you adding the view of DetailViewController into this mapviewcontroller's view. Don't you think right approach would be to either presentModalViewController or pushNavigationController?
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detailView.locationPoint = locationPoint;
detailView.locationCoordinate = locationCoordinate;
//[self.view addSubview:detailView.view];
[self.navigationController pushViewController:detailView animated:YES];
//OR
[self presentModalViewController:detailView animated:YES];
[detailView release];
You are getting the error because you are only using the view and deallocating the view controller immediately and hence tableview datasource and delegates are hitting a nil object.
Views do not retain their view controllers. Someone needs to retain the VC or else it will get released, and then the app will crash when the view makes a call into its delegate. When you use a navigation controller, the navcon has a stack of view controllers that it retains. Likewise with presentModalViewController, the OS takes care of retaining the detail VC.
Adding a detail view as a subview is not the normal way to navigate to a new view. Instead, one either uses a navigation controller and [navcon pushViewController::], or a modal subview and [self presentModalViewController::]. If the detail view occupies only a portion of the parent view, then it is normal to retain the view controller for the subview within the parent controller. That is, within the parent VC (your map class) add a property for the detail VC. Actually, it's more common to not even use a VC for a subview, but rather for screen-filling detail views.
I ran into something odd today that maybe someone knows something about. I have a subclass of UIViewController and its associated NIB. I set the labels in the UIViewController methods and all that works fine.
Now from another class, I create that ViewController again because I want to reuse it. I do this:
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
vc.titleLabel.text = #"testing";
vc.myTextLabel.text = #"yo";
self.navigationController pushViewController:vc animated:NO];
[vc release];
This does NOT work. I have no idea why this does not work. I would think I would set all the labels, then show the view controller by pushing it onto the stack.
However, if I do this:
[vc.view setNeedsDisplay]; // why here???
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
vc.titleLabel.text = #"testing";
vc.myTextLabel.text = #"yo";
self.navigationController pushViewController:vc animated:NO];
[vc release];
This DOES work. This does not make sense to me. I thought setNeeds Display was called AFTER a view needs to be redrawn. If I move setNeedsDisplay to the end of the block it does NOT work. It only works at the beginning of the block which is very odd to me. Any one encounter this before or know why it works this way? Thanks.
The reason is that a view controller's view is lazily-loaded. This means the controller's view is only loaded from a nib (or via -loadView) when you access the view property for the first time. If you attempt to access the labels before the view has been loaded, they will be nil and any messages you send to them will be no-ops.
So to force the view to load, you can do this:
/* make sure the view is loaded */
[vc view];
/* Access the label properties */
vc.titleLabel.text = #"testing";
However, forcing the view to load may not be a good idea in all situations, especially if the view controller is not going to be displayed immediately and you want to save memory.
In this case you can create the labels in the controller's init method so they always exist, and add them to the view controller's view manually in -viewDidLoad, rather than in your nib. This will allow the standard lazy-loading behaviour to work, but users of your class can still set properties on the labels before the view is loaded.
An alternative is to expose simple NSString properties with associated ivars on the view controller to represent any titles or text in the view. Then in your -viewDidLoad you can set the text of the labels to the value of these properties. Users of your view controller can then set these properties before the view has loaded.