IB linking question - controller to view

Discussion in 'iOS Programming' started by dantastic, Feb 2, 2011.

  1. dantastic macrumors 6502a


    Jan 21, 2011
    Hello again,

    This is a pretty fundamental question and I don't get how it works.

    I have been coding away at individual chunks of an app and I've come to the point where I need to pull stuff together and I've run in to a wall here. I realize I may have been doing this completely wrong to this point.....

    My base app consist of a main view with an info button. hit the info button and we flip over to a new view with a navigation controller on top that has 2 buttons. - simple.

    I also have a UITableViewController where all the cool stuff is happening. I want to put the UITableView of this UITableViewController into what is left of the UIView below the navigation bar.

    I have added the controller to the nib but how do I get the 'table view' under 'Settings Table View' to sit on my view?

    I have tried to add a new table view to the view and linked up the delegate & dataSource. (screenie 2). This is not working and is crashing but without any messages. I imagine that the TableViewController is not initialized.

    I've kindof hit the wall here. How do I do this?
    Back to basics as well, have I been doing this right or should I have approached this completely different?

    Attached Files:

  2. dantastic thread starter macrumors 6502a


    Jan 21, 2011
    I have once again had more luck abandoning IB.

    I have removed all references I did and reverted to screenie 1 in the first post.

    Then in the controller where I have the view I want to add my controller to I have done this:

    	SettingsTableView *settingsView = [[SettingsTableView alloc] initWithNibName:@"SettingsTableView" bundle:nil];
    	[self.view addSubview:[settingsView view]];
    And the view loads - but it loads on top of the navigation bar so that's no good. I guess my first question stands.
  3. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    I'd probably favor a non-IB approach for this as well. It's probably just a matter of setting the frame, especially the origin, for the table view correctly. (Exercise left to the reader: set the autoresizing mask appropriately as well. :) )

    But I would make sure you are instantiating a table view controller instead of just a table view. Something like:
    	SettingsTableViewController *settings = [[SettingsTableViewController alloc] init];
    	self.settingsTableViewController = settings;
    	settings.view.frame = CGRectMake(x, y, width, height);
    	[self.view addSubview:settings.view];
    	[settings release];
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    The navigation controller has a root view controller. I recommend that you build the Navigation-based application template and see how things are hooked up in that app. Do it the same way.
  5. dantastic thread starter macrumors 6502a


    Jan 21, 2011

    That is pretty much spot on what I ended up doing.

    I'm brand new to objective-c and the interface builder in particular. Is it common that there are tasks the IB simply can't do? What can be done in the IB gets done real quick so it's a massive time saver when you can use it.

    I suppose, should I spend more effort trying to learn IB or are there things like this task that are just better suited to do programmatically?
  6. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Code like this is almost never correct:

    [self.view addSubview:someviewcontroller.view];
    There are three container view controllers provided by Apple , UINavigationController, UITabBarController, UISplitViewController. It is not recommended to have more than one view controller that is controlling part of the screen at any one time (except for UISplitViewController, which is designed to have two at a time).

    Use Apple's container view controllers in the way they were intended to be used and things will just work. Don't, and you'll likely have problems. The problems are usually related to rotation of the device.

    The example mentioned in this thread should work just fine in IB if the view controller hierarchy is set up the way the navbar app template does it.
  7. dantastic thread starter macrumors 6502a


    Jan 21, 2011
    Right, so I am doing it wrong. I was expecting as much.

    I will digest for a bit and get back with followup questions. Thanks!
  8. dantastic thread starter macrumors 6502a


    Jan 21, 2011
    Ok, take 2 at this. Using a "utility application" template in xcode.

    Main view, no problem. Touch the info button and the second view is loaded.

    The template provide you with a UIView with a UINavigationBar. The controller created extends UIViewController.

    I have copy/pasted all my code from my old controller that was extending UITableViewController into the new one created that extends UIViewController.

    The tableView holds cells with a bunch of buttons each and in order to figure out which cell and button the request came from I've the following and it's somewhat broken in the new controller:

    - (IBAction)buttonPressed:(id)sender event:(id) event{
    	UIButton *button = (UIButton *)sender;
    	NSSet *touches = [event allTouches];
    	UITouch *touch = [touches anyObject];
    *	CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    *	NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    	if (indexPath != nil) {
    *	"Request for member 'tableView' in something not a structure or union"
    So for this to work I need to be able to be able to call the tableView within the view itself. Something like self.view.tableview - is this correct approach? If it is, how do I do it?

    Or if this is wrong, how do I figure out which button in which cell was pressed?
  9. dantastic thread starter macrumors 6502a


    Jan 21, 2011

Share This Page