manually start a view controller using storyboards. - iphone

I am trying to manually start a view controller using storyboards.
That is written in code as opposed to wiring it up. I tried:
self initWithNibName:(NSString *) bundle:(NSBundle *)
but I am not sure if they are classed as nibs or what they are classed as in storyboards. Any help would be great thanks!

Sounds like you are looking for this method:
AlertContainerViewController *alertContainerViewController =
[[UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:NULL]
instantiateViewControllerWithIdentifier:#"alertContainer"];
You need to give the ViewController object in the Storyboard a unique identifier and specify the subclass. You do both through the Property Inspector.

Related

Create a storyboard in PhoneGap 1.7

I have a question about using Xcode storyboard in the PhoneGap 1.7 project.
I have created an empty PG project, added a storyboard file to the project and dragged a View Controller onto the storyboard. But I do not find the wayto bind this controller to the variable created by PhoneGap framework:
CDVViewController* viewController;
How do I bind it if I can?
Thanks in advance.
Make sure you bind the ViewController in InterfaceBuilder to the CDVViewController class or your own subclass. It might also be useful to add a webview in the IB and bind it to the CDVCordovaView of the above view controller.
One thing you need to take care of is the fact that the CDVViewController does not implement the initWithCoder: method (as of Cordova 2.2.0), which is required for Storyboard usage. Add the following method to CDVViewController, so your view controller get's correctly initialized when instantiated from Storyboards:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self __init];
}
return self;
}

UIView presented with Black screen, fix?

I call a View to be presented with the following code:
#import "infoView.h"
...
infoView *viewInfo = [[infoView alloc] initWithNibName:nil bundle:nil];
viewInfo.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewInfo animated:YES];
But when it is presented in run-time the view that is loaded turns out black.
Currently I am using storyboard, but I need to use this code, for it is a lot more efficient in my case, because I am dealing with multiple views!
It works fine if I connect it via StoryBoard.
I should be seeing 2 labels, 1 UITextView, and 2 UIButton.
The view was created using StoryBoard, when the .m and .h files for the view where created I did not add a .xib for it. And also it is linked through the "Custom Class" section in StoryBoard.
Thanks, hope someone can help!
It's generally pretty bad form to mock people who are taking the time and effort to help you.
Naming is important it makes your code easier to work with and allows other people to use it. Not following the conventions for the language you are working in is dangerous and means that your code is not compatible with other developers as things are interpreted differently.
If you look at the docuemntation for UIViewController you'll see this note in the initWithNibName:bundle: method description
If your app uses a storyboard to define a view controller and its associated views, your app never initializes objects of that class directly. Instead, view controllers are either instantiated by the storyboard—either automatically by iOS when a segue is triggered or programmatically when your app calls the storyboard object’s instantiateViewControllerWithIdentifier: method. When instantiating a view controller from a storyboard, iOS initializes the new view controller by calling its initWithCoder: method instead. iOS automatically sets the nibName property to a nib file stored inside the storyboard.
Therefore you are instantiating your controller wrong, the storyboard should be instantiating it. Which is done like this (naming corrected)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:[NSBundle bundleForClass:[self class]]];
InfoViewController *infoViewController = [storyboard instantiateViewControllerWithIdentifier:#"InfoViewController"];
infoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self infoViewController animated:YES];
Side note
infoView is a bad name for the class not only because you didn't start with a capital but also because it's completely deceiving. Anyone reading this would assume that InfoView is a subclass of UIView not UIViewController.

iOS3, iOS4, xibbed UIViewController and displaying data

I'm writing an app that will run on iOS3.0 and up.
The app has a custom UIViewController (say), which I'm instantiating from a .xib file. It's view comprises a single UILabel, which I've correctly declared and synthesized etc. in my custom UIViewController header and implementation files.
I'd like to set the text of this UILabel dynamically, and for it to be shown to the user by the time my UIViewController appears. For sake of argument please assume the text setting method is expensive.
The catch is on iOS3.0 with my UIViewController at least, -(id)initWithNibName:(NSString *) aNibNameOrNil bundle:(NSBundle *) aNibBundleOrNil returns before -(void)viewDidLoad does, but on my iOS4.0 device it's the other way around. The text label can therefore be nil when I don't want it to be.
Thus, I don't know where I can set the text in such a way as to keep both iOS3.0 and iOS4.0 happy.
Any advice here?
Can you please explain how your label comes out to be nil?
If you have taken an outlet for the label, then in either case, in the - (void)viewDidLoad method it cannot be nil, provided the label outlet is properly connected in the xib file.
If you have not taken the label outlet and doing it by code, then instantiate the label again in - (void)viewDidLoad method and set its text right over there and then add it to the view controller's view.
For more information - read the - (void)viewDidLoad and - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle method in the UIViewController class reference

UIViewController issue

I want to get objects values from a UIViewController.
I am using this
SecondViewController *newObject=[[SecondViewController alloc] init];
But newobject has textview, its value is zero. How can I access the textview?
you have to make the textview as a property to be able to access it outside.
Looks like the initialization process is incorrect:
If you use NIB file for you view then you should use
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
to initialize your controller. If you create your view programmatically, you must override controller's -loadView method. (see UIViewController reference for details)
Edit: Make sure that your textview is connected to some controller's outlet in the nib file.

UIViewController -viewDidLoad not being called

Being new to Cocoa, I'm having a few issues with Interface Builder, UIViewController and friends.
I have a UIViewController subclass with a UIView defined in a xib, and with the controller's view outlet connected to the view. The xib's "file's owner" is set as myViewcontroller subclass.
In this one instance, the following code to load the controller/view (from the main view controller) doesn't work as expected:
if ( self.myViewController == nil )
{
self.myViewController = [[MyViewController alloc]
initWithNibName:#"MyViewController" bundle:nil];
}
[self.navigationController
pushViewController:self.myViewController animated:YES];
In MyViewController's methods, I have placed breakpoints and log messages to see what is going on:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
NSLog(#"initWithNibName\n");
}
return self;
}
-(void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad\n");
}
Expected result
Both -initWithNibName and -viewDidLoad methods are called, and myViewController's view is displayed.
Observed result
Only -initWithNibName is called, the view is not displayed.
Have I missed something? Can anyone recommend anything to check? (Particularly in the wondrously opaque Interface Builder tool).
RE: SOLUTION FOUND!!!!!
Indeed that seems to be a working solution, however the real trick is not in setting the view.hidden property to NO, what makes the view load from the nib file is the calling of the UIViewController's view method, the view only actually gets loaded from the nib when the view method is called for the first time.
In that sense, a simple [viewController view] message would force the view to load from the nib file.
Ok, I have a partial answer - maybe the gurus can explain some more. The problem is:
[self.navigationController pushViewController:myViewController animated:YES];
Looking more closely, in this case self.navigationController is nil - so the push message is going no-where.
Instead, if I send:
[self.view addSubview:self.myViewController.view];
Then the view appears and -viewDidLoad is called.
I'm not entirely sure why self.navigationController is not set in this instance - the only thing I can think of is that self is a subclass of UIViewController rather than UITableViewController (where the pushViewController code came from).
Also, silently allowing messages to go to nil seems like a bad idea, although these answers say otherwise. See also my question here.
Final edit:
Answers in comments below, I've realised the display function that I was actually after (given myViewController is modal) is:
[self presentModalViewController:myViewController animated:YES];
Thanks everyone for their helpful responses.
SOLUTION FOUND!!!!!
Even something as innocuous as this makes the viewDidLoad method call happen.
Insert this right after alloc initWithNibName
viewController.view.hidden = NO; //calls viewDidLoad
make sure that the view outlet in File's Owner (your viewController subclass) is connected to the actual view (i.e. the 480X320 canvas you see on your screen that you use to build your UI)
Chances are that you might not have linked the supposed ViewController in main.storyboard from the Identity Inspector to the custom class you created. You might be able to navigate to that controller from other view controllers via segues but any of viewDidLoad(), viewWillAppear() etc. won't be executed.
Simply use
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
//Your Code here
}
instead of the viewDidLoad method.
Another reason, somewhat obvious in retrospect: if viewController.view is set in code, then the viewDidLoad event will not trigger.
It looks like a capitalization problem to me. You're referencing the class MyViewController instead of the property myViewController in the call to pushViewController.
Check your run log for errors. Almost certainly, the NIB is not loading, and there should be an error to that effect. The most likely cause for that is failure to put it in the bundle. Look in your "Copy Resources" build phase and make sure that the XIB is actually being copied. Build for the simulator, and go down into the build directory and make sure that the NIB is in the .app bundle.
Apart from other answers here,
It often happens when the identifier with which you instantiate your ViewController from the storyboard is incorrect. For e.g.
[[self getStoryboard] instantiateViewControllerWithIdentifier:MyVC];
If MyVC is the identifier of some other ViewController, this might happen.
OP is using nib instead of storyboard here. But the answer applies.
The page has been presented but not visible in Debug view hierarchy & in device(simulator also), issue happens based on and
i found the fix:
func viewWillLayoutSubviews{
if day == true{
self.view.backgroundColor = .clear
}else{
self.view.backgroundColor = .blue
}
}
Don't try to implement the self.view (viewcontrollers view) in function of layoutsubviews. So better use self.view in viewwillappear or viewdidload. This issue happens starts from v-14 devices.
Hope it works for you too.