67986

Multiple Views in Xcode 4.2

Question:

I'm having a lot of trouble finding a tutorial for implementing multiple views in Xcode 4.2 without storyboard, this is for a class so I can't use storyboard yet. I'm just trying to have a 2nd view with a UIPicker come up when a button is clicked in the main view, I just can't find one for this version of Xcode and it's different enough from the older versions to confuse me.

Any help appreciated if someone can give me a quick description of what I need to do this or a newer tutorial I'd appreciate it :)

Answer1:

I think you should read the <a href="https://developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html" rel="nofollow">UIView Programming Guide</a> to get a good handle on how UIViews work exactly. I find nibs/storyboard are really great at confusing new iOS developers.

In essence, a UIViewController has 1 view which you set in the viewDidLoad or loadView method by using the [self setView:someUIView]. You add more stuff to the screen by adding UIViews as a subview of the viewcontroller's "Main" view. For example

-(void)loadView { // Create a view for you view controller UIView *mainView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self setView:mainView]; // Now we have a view taking up the whole screen and we can add stuff to it // Let's try a button, a UIButton is a subview of UIView UIButton *newButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect]; // We need to set a frame for any view we add so that we specify where it should // be located and how big it should be! [newButton setFrame:CGRectMake(x,y,width,height)]; // Now let's add it to our view controller's view [self.view addSubview:newButton]; // You can do the same with any UIView subclasses you make! MyView *myView = [[MyView alloc] init]; [myView setFrame:CGRectMake(x,y,width,height)]; [self.view addSubview:myView]; }

Now here we have our viewController who'se view is just a plain UIView which in turn has 2 subviews; newButton and myView. Since we created the MyView class, maybe it contains subviews as well! Let's take a look at what a UIView subclass could look like:

// Here is the init method for our UIView subclass - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Let's add a button to our view UIButton *newButton2 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]; // Of course, the frame is in reference to this view [newButton2 setFrame:CGRectMake(x,y,width,height)]; // We add just using self NOT self.view because here, we are the view! [self addSubview:newButton2]; } return self; }

So in this example we would have a view controller who'se view now contains 2 button! But the view structure is a tree:

mainView / \ newButton myView \ newButton2

Let me know if you have any other questions!

Matt

Recommend

  • how to reuse the same table view in different views?
  • lights animation in objective-c
  • How to use Xcode Extract refactoring feature?
  • How to export Selenium Test Suite/Case as C#
  • Render QGraphicsScene according to zoom level
  • Apple review objection Invalid directory name for Plugins folder (Child Browser Cordova)
  • Chef recipe for RoR Heroku
  • Change URL in browser without redirection with htaccess
  • Update web.config file in asp.net
  • Accessing music files into my application programatically
  • Geolocation method to see if user in specific area
  • how to convert open xml representation of word document into RTF format using c#.net 4.0?
  • Encode Byte array to JPEG image in Objective-C
  • How do I compile a C/C++ program through windows command prompt?
  • Graceful pod termination
  • Guava how to copy all files from one directory to another
  • SQL - Select lowest values with group by and order by?
  • Java Application vs. Java Desktop Application in Netbeans [duplicate]
  • Cypher - matching two different possible paths and return both
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • QLPreviewController hide print button in ios6
  • Django rest serializer Breaks when data exists
  • How to rebase a series of branches?
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Uncaught Error: Could not find module `ember-load-initializers`
  • Different response to non-authenticated users and AJAX calls
  • Azure Cloud Service Web Role web pages do not load
  • C# - Serializing and deserializing static member
  • Incrementing object id automatically JS constructor (static method and variable)
  • what is the difference between the asp.net mvc application and asp.net web application
  • Do create extension work in single-user mode in postgres?
  • Confusion with PayPal's monthly billing cycle
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • SQL merge duplicate rows and join values that are different
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • How to load view controller without button in storyboard?