Iphone-Creating Views from Code)

Source: Internet
Author: User

Create a view-based project named ViewTest;

Write the following code in the ViewTestViewController. m file viewDidLoad method:


[Cpp] // create a view UIView object to obtain the initialization screen size. UIScreen mainScreen indicates the internal screen of the device and the screen area and frame point of the applicationFrame application.
UIView * view =
[[UIView alloc] initWithFrame: [UIScreen mainScreen]. applicationFrame];

// Set the background color: Gray
View. backgroundColor = [UIColor lightGrayColor];
// Create a tag
CGRect frame = CGRectMake (10, 15,300, 20); // you can specify the position and size of a tag. x y width height indicates the upper left corner and size.
UILabel * label = [[UILabel alloc] initWithFrame: frame]; // create and initialize a tag and set the size to frame.
Label. textAlignment = UITextAlignmentCenter; // The align direction of the text in the label is center.
Label. backgroundColor = [UIColor clearColor]; // the background color of the label is transparent.
Label. font = [UIFont fontWithName: @ "Verdana" size: 20]; // the size of Verdana body 20 of the label text
Label. text = @ "This is a label"; // label text
Label. tag= 1000;
// Create text
Frame = CGRectMake (10, 70,300, 30 );
UITextField * text = [[UITextField alloc] initWithFrame: frame];
Text. backgroundColor = [UIColor whiteColor];
Text. text = @ "I'm ren haili ";
// Create button
Frame = CGRectMake (10,120,300, 50); // the size of the button position
UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Button. frame = frame;
[Button setTitle: @ "Click Me, Please! "ForState: UIControlStateNormal];
Button. backgroundColor = [UIColor clearColor];
Buttons. tag = 2000;
[Button addTarget: self
Action: @ selector (buttonClicked :) // responds to the event
ForControlEvents: UIControlEventTouchUpInside];
// Add tags, text, and buttons to the view
[View addSubview: label];
[View addSubview: button];
[View addSubview: text];
Self. view = view;
// Create a view UIView object to obtain the initialization screen size. UIScreen mainScreen indicates the internal screen of the device and the screen area and frame point of the applicationFrame application.
UIView * view =
[[UIView alloc] initWithFrame: [UIScreen mainScreen]. applicationFrame];

// Set the background color: Gray
View. backgroundColor = [UIColor lightGrayColor];
// Create a tag
CGRect frame = CGRectMake (10, 15,300, 20); // you can specify the position and size of a tag. x y width height indicates the upper left corner and size.
UILabel * label = [[UILabel alloc] initWithFrame: frame]; // create and initialize a tag and set the size to frame.
Label. textAlignment = UITextAlignmentCenter; // The align direction of the text in the label is center.
Label. backgroundColor = [UIColor clearColor]; // the background color of the label is transparent.
Label. font = [UIFont fontWithName: @ "Verdana" size: 20]; // the size of Verdana body 20 of the label text
Label. text = @ "This is a label"; // label text
Label. tag= 1000;
// Create text
Frame = CGRectMake (10, 70,300, 30 );
UITextField * text = [[UITextField alloc] initWithFrame: frame];
Text. backgroundColor = [UIColor whiteColor];
Text. text = @ "I'm ren haili ";
// Create button www.2cto.com
Frame = CGRectMake (10,120,300, 50); // the size of the button position
UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
Button. frame = frame;
[Button setTitle: @ "Click Me, Please! "ForState: UIControlStateNormal];
Button. backgroundColor = [UIColor clearColor];
Buttons. tag = 2000;
[Button addTarget: self
Action: @ selector (buttonClicked :) // responds to the event
ForControlEvents: UIControlEventTouchUpInside];
// Add tags, text, and buttons to the view
[View addSubview: label];
[View addSubview: button];
[View addSubview: text];
Self. view = view;
A warning box is displayed for the event response method:


[Html]-(IBAction) buttonClicked: (id) sender {
UIAlertView * alert = [[UIAlertView alloc]
InitWithTitle: @ "Action invoked! "Message: @" Button clicked! "
Delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil];
[Alert show];
}
-(IBAction) buttonClicked: (id) sender {
UIAlertView * alert = [[UIAlertView alloc]
InitWithTitle: @ "Action invoked! "Message: @" Button clicked! "
Delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil];
[Alert show];
}
Given:

 

 

 

You can use code to write any view interface and set attributes. Of course, this is not a simple method and it is not convenient to drag it directly, however, this is done to get familiar with the code and know how to implement it. Each control has many attributes and needs to understand how to use them. I am not afraid of trouble during the learning process. I hope to study hard with you! What's wrong? Please point out more !!!



From Ren haili (3G/mobile development)

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.