Reputation: 136
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. :)
Upvotes: 1
Views: 161
Reputation: 8805
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;
}
}
Upvotes: 1
Reputation: 5361
To do what your suggesting, you will not be ctrl+clicking from cells.
You need to set up UITableViewDelegate.
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.
Upvotes: 1