Help with Storyboard and Navigation Controllers

Discussion in 'iOS Programming' started by MickeyT, Jul 27, 2012.

  1. MickeyT macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #1
    I have read some of the Apple documentation on Navigation based applications and found it much like navigating through one; after drilling down through several "Read more on ....." chapters I'm now a bit lost.

    Firstly, I'm not sure I fully grasp how to simply add another view to a project correctly:

    Is it the case that you should always create a new view by adding it from the File menu so that you have an .xib file attached to the .h and .m files? If so, when you first create a new project and you have the first view on the storyboard already, along with a .h and a .m file for it, where is the related .xib file? Is there not one because the view is not a custom class?

    Secondly, if I am creating views in this way so that I have an .xib file to edit, am I correct that to add a custom class view to storyboard that you add a UITableView, for example, and then select my custom Table View class from the drop-down box in the inspector? If so, if I wanted to edit the appearance of something on the view do I do it in the .xib file or in the view I added to storyboard? I ask this because I can't seem to edit the properties of table cells in a custom table view I have created via the .xib file. There is just a list of example cells with "Brea", "Burlingame, "Canoga Park" etc that can't be selected. Editing them on the storyboard worked, but it seems to be counterintuitive if the custom class is meant to be a table view all set up and calibrated for how I want it to look.

    Thirdly, after adding a detail UIView, when I click on something in my table view in the simulator, it loads the view fine and even changes the title of the navigation bar. However, nothing else on my custom UIView appears (which should be an image in a UIImageView, and text in a label). The code I am using to do this, which is inside the TableView .m file is:


    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Navigation logic may go here. Create and push another view controller.
        
        TGDetailViewController *detailViewController = [[TGDetailViewController alloc] initWithNibName:@"TGDetailViewController" bundle:nil];
        
        _currentDrawnCard = [_deck drawCard: [NSNumber numberWithInt:indexPath.row + 1]];
        
        detailViewController.title = _currentDrawnCard.characterName;
        
        [detailViewController giveCard:_currentDrawnCard];
        
         // ...
         // Pass the selected object to the new view controller.
         [self.navigationController pushViewController:detailViewController animated:YES];
        
    }
    ...and then in the .m of the UIView:

    Code:
    -(void)giveCard:(Card *)card {
        
        _activeCard = card;
        
        _characterPictureView.image = _activeCard.characterStill;
        
        _testText.text = [NSString stringWithFormat:@"%d",_activeCard.strength];
        
    }
    If I haven't provided a key piece of information that you would need to work out what I'm doing wrong then please let me know; I'm not 100% sure on what is relevant at this point.

    Thank you.
     
  2. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #2
    Anyone?

    Main questions are:

    - Correct way to add new views to storyboard and have them set up correctly with .h and .m files
    - Why is there no .xib file when a new project is created even though there as a view already created on the storyboard?
    - Why can't table view cells be edited in a table view controller .xib file?
    - How do you push the detail view from a table view root view controller when it has been added to storyboard too? I have linked them via the Segue push connection, but when the detail view loads there's nothing in it - no image and label has no text. However, the title of the navigation bar changes (see code snippets)

    Really appreciate any guidance anyone has. Thank you.
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    You're confused. Starting with iOS 5, there are 2 distinct ways to save your view hierarchy to a file: XIB files and storyboards.

    Both are still supported, and you can mix and match, but usually you use one or the other in a given program.

    XIB files are set up to create a single object graph, like a view controller's views.

    Storyboards are intended to hold multiple objects. There are methods that let you instantiate view controllers and other objects from a storyboard file, usually with a unique string identifier.

    Storyboards also include segue objects, a new concept introduced in iOS 5. A segue represents a link between 2 view controllers and a specific transition to use during that transition.

    I have much more experience with XIB files, and have only used storyboards a little. Lots of iOS developers who've been doing it for a while are in the same situation. We know XIB files, they meet our needs, so we continue to use them.

    Most of your post s confused, and talks about creating XIBs and loading them from a storyboard, which is wrong. A given view controller's views come from one or the other, and will never be defined or used from both.

    If you are going to learn storyboards, you should probably pretend XIB files don't exist. Learn how to use Storyboards for everything.

    Then go back and do the same thing with XIB files. Do everything with XIBs and pretend Storyboards don't exist.

    Once you've done those 2 things, you will understand both approaches, and can decide on a project-by-project basis which is better.
     
  4. MickeyT thread starter macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #4
    You're right. I am confused.

    I will bin my project and start again without storyboards. I'm sure I recall being able to choose not to use them. The books I have bought a little while ago might then be useful because they won't be out of date in that respect.

    Thank you for responding.
     

Share This Page