IOS learning-HelloWorld

Source: Internet
Author: User

IOS learning-HelloWorld
1. Create a project named ButtonFun

In xcode, select File --> New --> Project, select Single View Application, and click next,

Enter ButtonFun in the Product Name column, select iphone in the Device column, click Next, select the project storage location, and click Create to Create a project.

2. Connect the xib and ViewController files

Xcode uses the story board. If you do not need to delete it directly, you can create a new ViewController. xib: File --> New --> File in the pop-up box, select User Interface on the left, View on the right, click Next, enter the name ViewController, and click create to create ViewController. xib File

Click the ViewController. xib File, click the first cube File's Owner on the left side, and enter ViewController in the Class of Custom class under the third tab on the top of the right side.

Add the following code to the viewDidLoad method in the ViewController. m file to load the xib file:

 

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    UIView *rootView =  [[[NSBundle mainBundle]loadNibNamed:@"ViewController" owner:self options:nil]lastObject];    self.view = rootView;}

 

3. Add Components

 

Open ViewController. xib and drag the Button and Label from the third tab in the lower right corner of xcode into xib,

On the menu bar, choose View> Assistant Editor> Show Assistant Editor to open two editing columns, showing ViewController. xib and ViewController. H files on the left and right,

Hold down the control key and click the Button. A blue line is displayed, and then drag it to ViewController. h. In the pop-up box, select Action for the connection item, and enter buttonPressed in the name column and click connect;

Drag the Button to the buttonPressed method again to bind the event;

In the same way, drag the Label to ViewController. h, select Outlet for the connection item in the pop-up box, and enter statusLabel in the name column and click connect;

The ViewController. h code is as follows:

 

@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet UILabel *statusLabel;- (IBAction)buttonPressed:(id)sender;@end

 

4. Implementation Method

Open the ViewController. m method now and there will be an empty method body:

 

- (IBAction)buttonPressed:(id)sender {}
Add code:

 

 

- (IBAction)buttonPressed:(id)sender {    NSString *title = [sender titleForState:UIControlStateNormal];    _statusLabel.text = plainText;}

Click "run" and click "OK ".

 

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.