IOS DEVELOP FOR DUMMIES

來源:互聯網
上載者:User

In this chapter, we’re going to write a slightly more complex application—one that will feature two buttons as well as a label

這個章節,我們要寫個稍微有點點複雜的應用程式,用於控制兩個按鈕和標籤的。

 

Creating Our Project

Launch Xcode and select  File  ➤  New ➤  New Project . Select the  Single View Application template, and then click  Next.

In the  Product Name field, type the name of  our new application,  Button Fun. The Company Identifier field should still have the value you  used in the previous chapter, so you can leave that alone. In the Class Prefix field, use the same value as you did in the previous chapter: BID.

select iPhone for  Device Family. We’re not going to use storyboar ds or unit tests, so you can leave both of those options unchecked. Howeve r, we do want to use ARC, so check the Use Automatic Reference Counting box. We’ll explain ARC la ter in the chapter.

 

Looking at the View Controller

The  Button Fun folder should contain four source code files (the ones that end in  .h  or .m) and a single nib file. These  four source code files implement two classes that our application needs: our application delegate and the view controller for our application’s only view. Notice that Xcode automatically added the prefix  we specified to all of our class names.

Button Fun檔案夾應該包括四個源檔案(兩個h 和兩個m檔案),這四個檔案實現我們應用需要的兩個類:app delegate和視圖控制器

 

Understanding Outlets and Actions

A controller class can refer to objects in a nib file  by using a special kind of property called an outlet .

控制器類可以通過nib檔案可以關聯到對象,是因為使用了一種叫做outlet的屬性。

interface objects in our nib file can be set up to trigger special methods in our controller class.  These special methods are known as  action methods (or just  actions).

nib檔案中的介面對象可以通過設定來出髮指定的方法,這個指定的方法我們指的是action

 Outlets

Outlets are special Objective-C class properties that  are declared using the keyword IBOutlet. Declaring an outlet is done in your controller class header file, and might look something like this:

@property (nonatomic, retain) IBOutlet UIButton *myButton; 

This example is an outlet called myButton, which can be set to point to any button in Interface Builder. The  IBOutlet keyword is defined like this:

#ifndef IBOutlet #define IBOutlet #endif

 

OUTLET CHANGES

In the first version of this book, we declared both a property and its underlying instance variable for our outlets. At that time, properties were a new construct in the Objective-C language, and they required you to declare a corresponding instance variable, like this:

@interface MyViewController : UIViewController {     UIButton *myButton; } @property (nonatomic, retain) UIButton *myButton; @end 

在這個版本中,apple已經把IBOutlet這個關鍵字從執行個體變數前面移除了。如下

@property (nonatomic, retain) IBOutlet UIButton *myButton;
 Actions

In a nutshell, actions are methods that ar e declared with a special return type, IBAction, which tells Interface Builder that this method can be triggered by a control in a nib file.

The declaration for an act ion method will usually look like this:

- (IBAction)doSomething:(id)sender; or like this: - (IBAction)doSomething; 

 

Cleaning Up the View Controller

Delete all the methods except for viewDidUnload . When you’re finished, your implementation should look like this:

#import "BIDViewController.h" @implementation BIDViewController  - (void)viewDidUnload {     [super viewDidUnload];     // Release any retained subviews of the main view.     // e.g. self.myOutlet = nil; }  @end 

 

Designing the User Interface

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.