how to change a ViewController to another that has no ".xib"? - iphone

I have a view controller that generates the ".xib" with Three20
I want a tableView Send me this ViewController has no ".xib" as you do?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.albumController) {
self.albumController = (AlThumbsViewController *)self; //AlThumbsViewController no have xib //Three20 is generated automatically with
}
[self.navigationController pushViewController:self.albumController animated:YES];
[self.albumController release];
self.albumController = nil;
}
Thanks for all.

MyViewController *view = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
[self.navigationController pushViewController:view animated:NO]; ---> you can use YES
or
[self.view addsubview:view.view];
and for opposite
[self.navigationController popViewControllerAnimated:NO];
[self.view romovefromsuperview];

If I understand you correctly, you want to transition from one view controller, to a second view controller which did not have a NIB/XIB file generated for it when you created the second view controller?
If so, you need to create a create a new XIB in xcode. Go to New File, User Interface, View. Then associate the new XIB file to the view controller by changing the Class Name in interface builder for your XIB to the same name as your view controller. Then set the view outlet in Interface Builder to the main view of the XIB. Then do initWithNibName on your view controller that does not have a XIB, and pass it the new XIB file you just created.
EDITED BASED ON COMMENT:
Step 1: Create a new file, User Interface, View. This will generate a stand alone XIB file.
Step 2: Go to your new XIB. Click File's Owner. Click Identity Inspector icon on right menu (the middle little icon). For "Class" change the name to the exact name of the view controller you want to link it to.
Step 3: Click file's owner. Click Connections Inspector icon on right menu (the right most little icon). Under Outlets, connect "view" to the main view of the XIB (just drag from the circle to the main window).
Step 4: Load your view controller like this:
MyViewController *view = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];

As in the picture?
error:
Undefined symbols for architecture i386:
"_OBJC_CLASS_ $ _AlThumbsViewController", Referenced from:
objc-class-ref in SocialesAlbumViewController.o
ld: symbol (s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use-v to see invocation)
see image here

Related

iPhone Storyboard Navigation

I'm new to iPhone development. I've done a couple of android apps now, so thought I'd try my hand on iPhone.
However, I really find the storyboard navigation to be counter-intuitive. I've created a basic tabbed app that has 2 screens, both of which contain a table view displaying various bits of data. When a user clicks on a cell of a table, I want it to open a new screen. Simple? Apparently not.
I've tried adding a new viewController and ctrl-clicking a table cell in one of my existing tableViews that is linked from the root tabbed view controller to add a seague to the new controller. Clicking a cell has no effect in the simulator.
I tried to link the new controller with a custom class to see if this was where I was falling down but even if I create a new Objective-C class that inherits from a UIViewController it won't appear in the drop-down box.
Unless I'm mis-understanding how the heirachy of view calls is supposed to work?
Any help appreciated!
Edit:
OK, so with the below advice I've managed to get further, but I'm still not quite there. I added navigation controllers for each of my tab view options. Then I ctrl-clicked the cell row to link to my new webview controller, choosing a selection seague set to push. I ctrl-clicked the webview to it's controller to make it an outlet delegate and added the following code to the row:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_webViewController == nil)
{
self.webViewController = [[[RSSWebViewController alloc] initWithNibName:#"WebViewController" bundle:[NSBundle mainBundle]] autorelease];
}
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
_webViewController.entry = entry;
[self.navigationController pushViewController:_webViewController animated:YES];
}
I also added:
#property (retain, nonatomic) IBOutlet UIWebView *webView;
to the new view controllers header file. When I click a row in the simulator, it throws the following exception:
2013-03-23 17:21:17.241 RSS[1330:c07] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle with name 'WebViewController''.
Thanks for the comments so far btw guys. :)
You need to have a UINavigationController as the view controller that is linked to the tab bar and then you have to link the UINavigationController to your current controller with the table as the root view controller and then link a cell to another view controller with a push segue. So you will have a navigation controller for each tab. The navigation controller is what enables it to "push" otherwise nothing happens because it manages the stack of view controllers as they are pushed and popped and the animations required to do them. I had this issue when I started as well.
Unless your cells are all static, you will probably need to transfer information from the current view controller to the new view controller such as which item was selected so you can tell the next view controller what to display. One way of doing this is having the next view controller be of a custom class with a property: #property (strong, nonatomic) NSString* message; and then in the view controller that has the push segue now can tell the next view controller right before it gets pushed.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"your-segue-name"]) // The identifier in the Storyboard for segue (click on the segue)
{
// Sender will be cell
NSString* message = [NSString stringWithFormat:#"cell %d!", [[self.tableView indexPathForCell:sender] row]];
YourViewControllerClass* destination = [segue destinationViewController];
destination.message = message;
}
}
To do what your suggesting, you will not be ctrl+clicking from cells.
You need to set up UITableViewDelegate.
See https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006942
Then, as mentioned above by Mike D, you need to use didSelectRowAtIndexPath.
This should point you in the right direction. I will check back when I am on my computer (on phone now) and if you've not got your problem solved, I can explain in more depth.

Xcode4: How to create a menu for a game?

I have a game with 4 game modes. the problem is that I want to create a main menu to choose a mode, because instead of that menu there is a TabBar. I'm trying to do that putting each mode in different .xib files, and create an other .xib file for the menu.
Menu.m:
-(IBAction)PlayMode1:(id)sender{
ViewController *Mode1 = [[ViewController alloc] init];
[self presentModalViewController:Mode1 animated:YES];
[Mode1 release];
}
with this I'm getting this error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '- [UIViewController _loadViewFromNibNamed:bundle:] loaded the "ViewController" nib but the view outlet was not set.'
I checked that "view" is linked to "View" on IB so I don't know what to do...
I'm not an advanced developer, thanks for helping!
You should try to do the following:
-(IBAction)PlayMode1:(id)sender{
ViewController *Mode1 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self presentModalViewController:Mode1 animated:YES];
[Mode1 release];
}
Seems like you forget to put initWithNibName inside the allocation of the view.
Also, make sure that your ViewController.xib is the correct name of the view controller and check if it has the correct outlets linked.
Check View Identity > Class Identity is set too. If not, type in the name of your controller, ViewController
EDIT
Remove everything in the xib, "Window" and "ViewController".
Drag an UIView on the left column and set its outlet.
Click on File's Owner and then, on the Inspector, Identity inspector section (third one from the left), type in the class of your view, `ViewController´.

iOS - Interface Builder Outlets Not Initialized

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.

Navigation based application with an add button

I created a new Navigation Based Application project.Then in MainWindow.xib I added a button to the navigationbar. I would like to push a new View onto the screen where I can enter information, which will be added as an object to the array of the UITableView.
But I don't know where to write the IBAction to link the button to (Appdelegate or the RootViewController)? Because as you see in the screenshot, it resides in MainWindow.xib because the RootViewController is merely a Table and doesn't contain the navigation. But in the document view of MainWindow.xib it is located under the RootViewController.
Do I have to create a new View Controller inside the XIB as well and create an IBOutlet for it?
I tried putting the code inside my AppDelegate and reference the button to the delegate but it doesn't work.
Please help. Thanks in advance.
See the screenshot here: http://i56.tinypic.com/5djbcm.png
When you ask yourself a question "where does this action belong" it's most probably a controller because controllers handle event flows in your app. Next question - "What controller is in charge when this action happens? What controller is most interested in this action?". Answer in your case is root table controller (RootViewController instance). Create an IBAction method in it which will push form controller (one you use to enter information) to navigation controller.
// somewhere in RootViewController.m
- (IBAction)addNewEntry {
NewEntryFormController *c = [[[NewEntryFormController alloc] init] autorelease];
// ...
[self.navigationController pushViewController:c animated:YES];
}

Pushing View from UITableView Problem

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.