Add View dynamically in Xcode Study Notes

Source: Internet
Author: User

XcodeStudy NotesDynamicAddViewThis document describes how to edit the. xib file by using the Interface Builder.ControlParts andWidgetIBOutlet and IBAction ). This chapter describesDynamicAddView, Do not use Interface Builder. Here we use the label and button examples:

Find the-(void) loadView method of XXXViewController. m in the new project, remove the comments, and add the following code:

 
 
  1. -(Void) loadView {
  2. // Create a UIView object
  3. UIView * view =
  4. [[UIView alloc] initWithFrame: [UIScreen mainScreen]. applicationFrame];
  5. View. backgroundColor = [UIColor lightGrayColor];
  6. // Create a label view
  7. CGRect frame = CGRectMake (10, 15,300, 20 );
  8. UILabel * label = [[UILabel alloc] initWithFrame: frame];
  9. Label. textAlignment = UITextAlignmentCenter;
  10. Label. backgroundColor = [UIColor clearColor];
  11. Label. font = [UIFont fontWithName: @ "Verdana" size: 20];
  12. Label. text = @ "label test ";
  13. Label. tag= 1000;
  14. // Create a button view
  15. Frame = CGRectMake (10, 30,300, 50 );
  16. UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  17. Button. frame = frame;
  18. [Button setTitle: @ "button test" forState: UIControlStateNormal];
  19. Button. backgroundColor = [UIColor clearColor];
  20. Buttons. tag = 2000;

/* The following call uses the C ++ format.

 
 
  1. button->addTarget(this->action, @selector(buttonClicked:), UIControlEventTouchUpInside); 

The intermediate action: And forControlEvent: are actually part of the function signature. @ Selector (buttonClicked :) is equivalent to a function pointer (a colon indicates that the function has a parameter). Here it points to the buttonClicked function.

That is, the button response function defined below */

 
 
  1. [button addTarget:self action:@selector(buttonClicked:) forControlEvent:UIControlEventTouchUpInside];  
  2. [view addSubview:label];  
  3. [view addSubview:button];  
  4. self.view = view;  
  5. [label release];  

Add the button response function to this file.

 
 
  1. -(IBAtion) buttonClicked:(id)sender {  
  2. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Action invoked”   
  3. message:@”button clicked”   
  4. delegate:self   
  5. cancelButtonTitle:”@ok”   
  6. otherButtonTitles:nil];  
  7. [alert show];  
  8. [alert release];  

The rectangle area of the label is CGRectMake (10, 15,300, 20); the coordinates in the upper left corner are 10, and the width and height of 15 are 300, 20, respectively.

The coordinates in the upper left corner of the rectangle area of the button are 10 and 30, and they overlap.

Here the occlusion is appended.ViewThe occlusion in the content is first added. So the button blocks the label. You can use

 
 
  1. [view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; 

To modify the occlusion. In my understandingViewFollowWidgetThe added index is incremented from 0. The index value is large when it is displayed.WidgetThe occlusion value is small. The above function exchanges the first twoWidget(In fact, only these two) index.

Summary:XcodeStudy NotesDynamicAddViewI hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.