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:
- -(Void) loadView {
- // Create a UIView object
- UIView * view =
- [[UIView alloc] initWithFrame: [UIScreen mainScreen]. applicationFrame];
- View. backgroundColor = [UIColor lightGrayColor];
- // Create a label view
- CGRect frame = CGRectMake (10, 15,300, 20 );
- UILabel * label = [[UILabel alloc] initWithFrame: frame];
- Label. textAlignment = UITextAlignmentCenter;
- Label. backgroundColor = [UIColor clearColor];
- Label. font = [UIFont fontWithName: @ "Verdana" size: 20];
- Label. text = @ "label test ";
- Label. tag= 1000;
- // Create a button view
- Frame = CGRectMake (10, 30,300, 50 );
- UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- Button. frame = frame;
- [Button setTitle: @ "button test" forState: UIControlStateNormal];
- Button. backgroundColor = [UIColor clearColor];
- Buttons. tag = 2000;
/* The following call uses the C ++ format.
- 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 */
- [button addTarget:self action:@selector(buttonClicked:) forControlEvent:UIControlEventTouchUpInside];
- [view addSubview:label];
- [view addSubview:button];
- self.view = view;
- [label release];
- }
Add the button response function to this file.
- -(IBAtion) buttonClicked:(id)sender {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Action invoked”
- message:@”button clicked”
- delegate:self
- cancelButtonTitle:”@ok”
- otherButtonTitles:nil];
- [alert show];
- [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
- [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!