I have searched this forum and looked at various solutions to the "black screen" view issue when a screen is loaded on the iPhone simulator, but I believe my problem is unique and thus I have not yet been able to find a solution.
Here is what is happening:
ViewB loads correctly when I link ViewA to ViewB using Interface Builder using "push" segue (drag line over, select "push");
the "push" is executed when a button on ViewA is pressed.
However, when I try to load ViewB programmatically when the button on ViewA is pressed, ViewB appears as a black screen. Here is what I am doing:
I am using the latest version of XCode;
-(void) loadView has been commented out of ViewB;
I created ViewA and ViewB using Interface Builder and there is no .xib file for either View, so I believe I cannot use the "initWithNibName" parameter (?) when programmatically alloc + init'ing ViewB (?) when the button is pressed.
I have a NavigationController, ViewA is the "RootView," and a ViewController for each of ViewA and ViewB.
The code I am using to programmatically load ViewB from the ViewA button press is as follows:
ViewB *viewB = [[ViewB alloc] init];
[self.navigationController pushViewController:viewB animated:YES];
Does anyone have any solutions? (P.S--this community is amazing!!!)
Thanks everyone!
Try initializing the ViewController with initWithNibName:#"" bundle:
Put the name of the XIB file for that view in the place of first argument without the extension ".xib" and set the bundle to nil or [NSBundle mainBundle]. You can further check on the Apple Documentation.
When you are using storyboards you should initialize your ViewController like this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle bundleForClass:[self class]]];
ViewControllerB *viewController = [storyboard instantiateViewControllerWithIdentifier:#"identifier"];
[self.navigationController viewController animated:YES];
In interface Builder you should set the identifier of the view controller you're try to initialize to "identifier"
(MianStoryboard_iPhone is of course your storyboard's name!)
probably [self navigationController] is nil, check it.
Probably your XIB is messed up. You can try to delete the viewcontroller.m[h][xib] and add a new one. Before do that, copy and past the content of the m and h file. After add the new empty viewcontroller, just past the contents. Unfortunately you'll have to assemble the xib again.
Related
I am new to iOS, can any one please help me to open and activity on Button click.
I have tried below methods, but the app remains on same ViewController, i am using Singleview Application type
Second *sec = [[Second alloc] init];
[self.navigationController pushViewController:sec animated:YES];
Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondV"];
[self.navigationController pushViewController:sec animated:YES];
can anyone please help me to solve this.
if you are using storyboard then Embed your viewcontroller to UINavigationController Editor->EmbedIn->NavigationController try this
If not using Storyboard, then following might help:
Second *sec = [[Second alloc] initWithNibName:#"Second" bundle:nil];
[self.navigationController pushViewController:sec animated:YES];
For Storyboard enabled code:
Second *sec = [self.storyboard instantiateViewControllerWithIdentifier:#"YourIdentifierForVC"];
[self.navigationController pushViewController:sec animated:YES];
BTW, I strongly suggest you to google the problem for solution & search SatckOverflow - first, before posting as a new question.
Also, you should reconsider the naming for the class. Just a suggestion.
to push to another view controller you need to set a UINavigationController in front of your UIViewController in storyboard. Your self.navigationController might be nil, that's why it is not pushing your view controller.
Therefore drag and drop a Navigation Controller to your storyboard and delete the default TableViewController (make sure to click on empty space in storyboard before selecting and deleting it) and connect the UINavigationController to your own 'single view controller' by right-clicking on the yellow arrow and connecting the rootViewController outlet with it).
Then, select the UINavigationController on your storyboard and in Attributes Inspector (the fourth symbol in the right panel) under the section View Controller select Is Initial View Controller.
First of all sorry for my bad english and Im pretty new in these forums and Xcode programing.
So, Im writing an IPhone app with Xcode 4.1, that has Login and Register stuff visualized with UIViewController. When Im logged in, I need to visualize TabBar with different views.
I tried a lot of stuff and watched a lot of tutorials, all of them just start with the TabBarController, but I don't need it from the beginning, I just need to call it later.
The right way I believe should be just create new file .h, .m and .xib, then add the TabBarController and do a relation between TabBarController - view and File's Owner - view... but it don't let me do this thing. Obviously it don't visualize the right window.
How is the right way to do it?
Please help me, before my hair fall off...
Use the UITabBarController as the root view controller, but display a modal registration / logon view controller over the top when the app begins.
Once the user has logged in, dismiss the modal view controller to reveal the tab bar controller below.
You just use this code in your login button click or next viewcontroller viewwillappers method
UITabBarController *tabbar1 = [[UITabBarController alloc] init];
firstViewcontroller *second = [[firstViewcontroller alloc] initWithNibName:nil bundle:nil];
second.title=#"";
SecondViewController *third=[[SecondViewController alloc]initWithNibName:nil bundle:nil];
third.title=#"";
thirdViewController *one=[[thirdViewController alloc]initWithNibName:nil bundle:nil];
one.title=#"";
tabbar1.viewControllers = [NSArray arrayWithObjects:one, second,third,nil];
tabbar1.view.frame=CGRectMake(0, 0, 320, 460);
[self.view addSubview:tabbar1.view];
I am sure it will work for you i always use this code for create tab bar in any view.
I have created a view in Interface Builder with some labels and text as IBOutlets. I can view this screen perfectly when I segue to it from another view that I have defined in my Storyboard.
However, I have another XIB and an associated UIViewController that I want to access that view from. Because my XIB is not in the Storyboard, I cant segue to it. Instead I have to execute the transition programmatically.
PlantDetailViewController *plantDetailVC = [[PlantDetailViewController alloc] init];
[self.currentNavigationController pushViewController:plantDetailVC animated:YES];
When this code is executed it transitions to the view but the view is just blank. I have debugged the code and it enters viewDidLoad and viewWillAppear however all my IBOutlets are NIL....so nothing it showing up on screen!
Can anyone tell me why they might be NIL and how I can initialize them?
Thanks
Brian
It sounds like you're saying you have a PlantDetailViewController set up in your storyboard, and some OtherViewController that was created outside of your storyboard. Now you want OtherViewController to instantiate the PlantDetailViewController that was set up in your storyboard.
Let's say your storyboard is named MainStoryboard.storyboard.
First, you need to set the identifier of the PlantDetailViewController in your storyboard. You do this in the Attributes inspector, under the View Controller section. Let's say you set it to PlantDetail.
Then, in OtherViewController, this should work:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
PlantDetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"PlantDetail"];
[self.currentNavigationController pushViewController:vc animated:YES];
-init doesn't load a nib file for you, if you want to load a nib use -initWithNibName:bundle:
If you use nib naming conventions you can pass nil to load a nib whose name matches your class and the default bundle, i.e. [[PlantDetailViewController alloc] initWithNibName:nil bundle:nil], see the docs for -nibName for details.
Basically, what I want is to be able to press a record in a table, and have it push to another view.
To do this, I created a nib file and a UIViewController subclass (for the "pushed" view). I set the nib file's "File Owner" to be the controller I created. EDIT: I also then set the "view" field of the controller to be the View. Then, in the view controller of the table that will push that view, I set the didSelectRowIndexAtPath: method to include the following:
SearchTableController *vc = [[SearchTableController alloc] initWithNibName:#"SearchTable" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
(where "SearchTableController" is the name of the UIViewController subclass and "SearchTable" is the name of the nib file)
However, when I run this code and click on the record, nothing happens- the app doesn't crash, but the view doesn't get pushed. The code is getting run, because it works when I NSLog(), but it doesn't seem to be pushing the view.
Thanks for any help in advance.
You are really close, but did you tie the view in the nib file to the view field of the view controller?
EDIT: So the view is connected, and your code looks just fine. I just pulled a piece of my own code:
// show theme settings
ThemeController * theme = [[[ThemeController alloc]
initWithStyle:UITableViewStyleGrouped] autorelease];
[[self navigationController] pushViewController:theme animated:YES];
Have you checked if self.navigationController is non-nil?
Couple of things I'd try at this point:
just hard-code the view to come up as the root view, verify it works
put a couple of NSLogs in the viewWillLoad, viewDidLoad, viewWillAppear in the client view controller
Best of luck.
I have an iPhone app that shows a simple view (View 1) that has a button. When the user presses this button, View 2 slides into view using the call
[self presentModalViewController:self.view2 animated:YES];
I want View 2 to support a navigation controller. All the code I find tells you how to set up a Navigation Controller App, but I can't figure out how to set this up using IB.
What I have done is to create a plain view2.xib file. I set the file's owner class to view2.
I add a navigation Controller to the XIB. I create an IBOutlet called view2Nav in view2.h for a UINavigationController. I link view2Nav to the NavigationController in view2.xib.
I then create a view3 class with view3.xib. I set the RootViewController in view2.xib to be of class view3 and set its NIB name to view3.
Then I go back and run the program. When I press my button on view 1, the app crashes as it tries to create view 2.
I know I must be missing a setting or something.
MySecondViewController *secondVC = [[MySecondViewController alloc] initWithNibName:#"MySecondViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondVC];
[self presentModalViewController:navigationController animated:YES];
[secondVC release];
[navigationController release];
Forget about IB. Do anything in code :) It is faster and you will exactly know why and how it works.
I'm not sure whether you can pass a self.view2 to presentModalViewController. If self.view2 is a subclass of UIViewController, you can. If it is a simple UIView, you shouldn't. If fact you can't at all.