Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically adding view controllers, too many?
    primarykey
    data
    text
    <p>This is my first iOS app.</p> <p>I’m making an app with a navigation controller and a tab bar controller. Each tab bar controller has a table view, where each cell may load another table, or maybe a custom view with data (like a bunch of labels and an image) - it depends on the content. I’m doing it programatically. </p> <p>Because I’m doing this to be as flexible as possible, I thought it might be a good idea to separate views from each other so a new one could be dropped in very easily if the design, layout or new data is added to one of the final views. I’m making this with the intent that I may change my mind at any time how it’s structured. However, it’s causing me to feel anxious that something isn’t right.</p> <p>The tabs represent related kinds of data (like music, movies, videos, for example) and the table cells represent similar attributes of the bigger tab based categories (album art, touring dates, band twitter, for example). Since all the information could be so different, I thought of doing it this way. At the very end of a table view cell selection would be a custom view with the appropriate data. It always starts with a table view, though.</p> <p><code>AppDelegate.m</code></p> <pre><code>ViewController1 *vc1 = [[ViewController1 alloc] init]; ViewController2 *vc2 = [[ViewController2 alloc] init]; ViewController3 *vc3 = [[ViewController3 alloc] init]; ViewController4 *vc4 = [[ViewController4 alloc] init]; ViewController5 *vc5 = [[ViewController5 alloc] init]; UITabBarController *tbc = [[UITabBarController alloc] init]; NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, vc3, vc4, vc5]; [tbc setViewControllers:viewControllers]; // nav controller is a property because another initial method returns the the view if conditions are met [self.nc = [[UINavigationController alloc] initWithRootViewController:tbc]; [[self window] setRootViewController:self:nc; [self.window makeKeyAndVisible]; </code></pre> <p>This sets a bunch of view controllers into the tab bar which is then put into the navigation controller which is loaded into the view. </p> <p>All of the <code>ViewController#.m</code> files look similar:</p> <pre><code>- (void) viewDidLoad { [super viewDidLoad]; self.tabBarController.title =@”Title”; TableViewController# *tvc = [[TableViewController# alloc] init]; UIView *new = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 420, 420); [self addChildViewController:tvc]; [new addSubview:tvc.view]; [self.view addSubview:new]; } </code></pre> <p>The table views then load their cells as normal. Currently, the cells don’t hook up to their destination views because this question came up. This works, but it feels really dirty and incorrect. The tab bar controller loads a view, whose soul purpose is to load another view? That can’t be right, but it’s the only way I could figure it out. I’d have to do this for every subsequent tableview?! I’m going to have a hundred view controllers, which freaks me out.</p> <p>Also, setting the CGRect seems to work fine, but I had to put an empty table footer in because no matter what I set the dimensions, it would always put cells underneath the tab bar. Very inelegant. While it seems to work fine on 3.5” vs 4” devices, I don’t know why, I set a static view size, right?</p> <p>What are some ways I can optimize this workflow before I get in too deep? If storyboarding is the answer, then please point me to a really good tutorial on it, I just can’t ever seem to get things to link up properly when I use storyboards.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload