Change views via code using xcode 4.2 and storyboard? - iphone

How would I go about changing views via code using xcode 4.2 and storyboard?
I'm trying to change views once a dynamic table view cell is clicked.
Since its dynamic I can't just link it with a segue...
I've tried changing using this code as suggested by another article on Stack Overflow but it doesn't work:
UIViewController *alertView = [self.storyboard instantiateViewControllerWithIdentifier:#"alertView"];
[self.navigationController pushViewController:alertView animated:YES];
Here's a snippet of my code, hope it helps!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row==0){
UIViewController *alertView = [self.storyboard instantiateViewControllerWithIdentifier:#"alertView"];
[self.navigationController pushViewController:alertView animated:YES];
NSLog(#"This code is being called...");
}

In your storyboard create a segue from your UITableViewController to your alertView. Make sure you do it by control dragging from the Controller (next to the First Responder icon) to your alertView rather than dragging from your table cell or something like that. Choose a segue type of 'Push'. Click on the segue's properties and make sure to give it an identifier like AlertViewSegue
Now you can programmatically invoke your segue with code like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row==0){
[self performSegueWithIdentifier:#"AlertViewSegue" sender:self];
}
}

this is one of the better storyboard tutorials and it happens to use tableviews.
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

Related

iPhone: TableViewController nibName error with "initWithNibName:bundle:"

I have a dynamic cell UITableViewController and I want to push a specific View Controller when someone taps a cell. So I'm trying to use the pre-made method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
But when I go into the simulator and tap a cell, this is what I get:
"NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle [...] with name 'AffichageVotesQuestionDuMomentViewController'.
Within the method, I have this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
AffichageVotesQuestionDuMomentViewController *detailViewController = [[AffichageVotesQuestionDuMomentViewController alloc] initWithNibName:#"AffichageVotesQuestionDuMomentViewController" bundle:nil];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
AffichageVotesQuestionDuMomentViewController is a custom view controller I created myself. I also tried creating another random one and I get the same type of error. If I set the initWithNibName: to nil, it segues to a view controller that has the navigation bar on top, but that's completely black.
Now, I'm a beginning iPhone programmer and have a very poor understanding of nibs, nib names and such. Any help is appreciated. Also, I didn't do any segue in the storyboard or anything, since it seems I'm programmatically creating the view controller and pushing that way. If there's a good way to do it with segue though, I'm okay with that too (but I need to know the row of the tapped cell).
Thank you!
Because you're using IB storyboards you should do something lik this i think:
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"mystoryboard" bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:#"AffichageVotesQuestionDuMomentViewController"];
... instead of using the "initWithNibName"-initializer which is used for the classical xib-based approach.

How to make the detailview and masterView communicate in the splitViewController?

I have a UISplitView with a masterView and a detailView. The masterView is a UITableViewController (inside a UINavigationControler) and the detailView is a UIViewController (also inside a UINavaigationController).
The UISplitView appears just fine with the masterView and an empty detailView. In masterView, I have this method:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (![self splitViewController])
DetailViewController*myDetailView = [[DetailViewController alloc] init];
Item *selectedItem = [items objectAtIndex:[indexPath row]];
[myDetailView setItem:selectedItem];
[[self navigationController] pushViewController:myDetailView animated:YES];
}
This works fine for iPhone.
For iPad, the detailView appears in the masterViewController, allbeit correctly, but the detailview remains empty.
How can I make myDetailview appear in the detailView (right side of the splitView)?
I recommend you to use Matt gemmell's MGSplitViewController
He has customized everything in the split view controller, you can get the master at right side using this and its just weird that you said that UISplitViewController is working fine in iPhone
To be clear UISplitViewController is specific for iPad and not for iPhone
So, please have a knowledge of iPad specific controllers like UIPopOverController and UISplitViewController
In Doc's, you can read this in the first line it self
Split view controllers are for use exclusively on iPad devices.
Attempting to create one on other devices results in an exception.

How to call a detail view from a table view within a Storyboard

I have following problem.
I have created a tab based Application with three Views and Viewcontroller.
FirstView(Start screen stuff), SecondView (Detailpage), ThirdView (Table for listing items).
(Connections from storyboard were set automatically).
In the third view a table is integrated and the content is displayed fine.
Now I would like to call the SecondView , when a row in the table is selected.
I also tried to add a forth View , outside the tabBar Controller, to get the Detailview, but this also did not help.
I have tried several tutorials and forum tips, but cannot get it working.
The class is set to the right ViewController, the identifier is set to detail.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"detail"];
[self.navigationController pushViewController:detail animated:YES];
detail.label1.text = #"It is working";
}
When clicking on the row, it becomes blue, but nothing happens. No error Message , nothing.
Hope that you can help :-)
OK, I have tried to "optimize" my design.
Still have the tab based Views, but when clicking on a row in the table, a new (not linked in Storyboard) view should appear to display the details of the selected quote.
I have created a view in the storyboard and called it "detailzitat"
I have created a DetailViewController.h/m as UIViewcontroller class
I have set the custom class to DetailViewController
I import the DetailViewController.h in the ThirdViewController.h
I have modified the didSelectRowAtIndexPath method in ThirdViewController.h accordingly.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:#"detailzitat"];
[self.navigationController pushViewController:detailVC animated:YES];
}
and my DetailViewController.m looks like
#import "DetailViewController.h"
#implementation DetailViewController
#synthesize label1, labeltext;
- (void)viewDidLoad
{
[super viewDidLoad];
labeltext=#"JUHU";
label1.text=labeltext;
}
But again, nothing happens, besides the row gets blue.
I do not understand. If I am using this code in a Non-Storyboard project, it is working.
What am I doing wrong ? Is there any tutorial for this combination within Storyboards ? Have not found one for this approach yet.
Try to learn from the different tutorials on the web, but the biggest problem is, most ones are not for iOS5 and I am not so good to transfer then.
Hope to get some hints :-)
You might want to think about your design. If I am understanding your description correctly, the user will be on the third tab, tap on a row in a table, and then you will be switching them back to the second tab. A navigation controller might be a more natural, less confusing, choice there.
But in any case, something like this will work sometimes:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailVC = [self.tabBarController.viewControllers objectAtIndex:1];
detailVC.label1.text = #"It is working"; // <- this will not work consistently!
self.tabBarController.selectedViewController = detailVC;
}
The problem with this is that while the user is on that third tab, it's possible that the second tab view controller's view is unloaded (due to memory pressure for example).
It's also possible the user went from tab 1 to tab 3 immediately and therefore the 2nd tab's view isn't even loaded yet at all. (To even test the above code you would have to select tab 2 and then tab 3.)
If the second tab's view hierarchy is not loaded, the label1 property will be nil, and so this will not work. A better strategy would be to create a new #property of type NSString* on the DetailViewController. And set that property instead of trying to set the label1 directly.
Then in your viewWillAppear: for the DetailViewController you can update your labels as needed. At that point of course you can be sure that label1 is loaded and connected to the correct UILabel.
I hope that helps.
I think problem is at self.navigationController. If your view is not inside the navigation controller this will not work. So what you do is create a new navigation controller object there and then use it to show your detail view.

Search Display Controller does not show searchBar

I created a simple TableViewController using the template offered by xCode.
Then I open the xib file of the TableViewController with Interface Builder and I drag/add a uisearchdisplaycontroller on the top part of the tableView.
xCode automatically creates and link all the outlets.
I save the xib file and I run the app but the searchBar is not displayed!
What else should I do to make appear the searchBar?!?
THANK YOU VERY MUCH!!!
I was having this exact issue. I would drag the search bar, it would "snap" into place on the table as a header, but it would never show.
I found that when you are presenting the next View you need to initWithNibName:
Example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewSubclass *dvc = [[UITableViewSubclass alloc] initWithNibName:#"UITableViewSubclass" bundle:nil];
[self.navigationController pushViewController:dvc animated:YES];
[dvc release];
}
This is also assuming you dragged the SearchDisplayController into the xib file. It will do all the necessary connections for you.
Hope that helps.

iPhone SDK: Pushed view controller does not appear

I want to add a detail view for one of the cells in my UITableView. I've created a new view controller SyncDetailViewController) and a NIB for the detail view. Didn't make many changes in the new class. Just added a label and outlet for it and connected nib with view controller.
Now I've added this code to my view controller (the one with UITableView, not the new one)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
SyncDetailViewController *dvController = [[SyncDetailViewController alloc]
initWithNibName:#"SyncDetailViewController" bundle:nil];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
}
The problem is that even if I select any row in the UITableView nothing happens. It doesn't produce any errors or warnings. I've added a breakpoint inside this method and it is reached so this code is executed.
Any ideas are much apprieciated.
"Nothing happens" sound so much like "sending a message to nil". Have you, by chance, checked that self.navigationController is not nil? Or that dvController is well initialized?