Use 1 for iOS-xib and 1 for iOS-xib

Source: Internet
Author: User

Use 1 for iOS-xib and 1 for iOS-xib

1. parsing and usage of File's owner:

1.

Storyboard: Describes the software interface. It is generated after iOS5.0.

Xib: Describes the software interface. It is the predecessor of storyboard.

 

2.

All resources in the project environment must be accessed through [NSBundle mainBundle], such as accessing the full path of images;

=== There is a method: loadNibNamed :( NSString *) owner :( id) option :( NSDictionary *) ===

 

3. If a control in xib wants to connect to the Controller:

1> the file's owner of xib must set the class as the corresponding controller;

2> loadNibNamed :( NSString *) owner :( id) option :( NSDictionary *) the owner must be set to the corresponding controller (because this parameter is not passed, the file's owner is still blank during running );

3> xib sets the class of the file's owner. Only then can the control in the xib find the corresponding method to call in the file's owner (class or object, such as the Dog object.

 

4. The essence of xib is xml. The parsing process is as follows:

Certificate ------------------------------------------------------------------------------------------------------------------------------------

5. File's owner steps:

 

Note: type matching means that the types set in the class of the File's owner must be the same as those of the owner parameter in loadNibNamed :( NSString *) owner option :( NSDictionary.

 

Ii. view encapsulation (custom view)

The Controller should not be used to listen for button clicks in the xib, because the xib is bound to the Controller, and the coupling is too strong, which is not conducive to expansion and reuse.

Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------------

For ease of viewing, briefly describe the overview of custom views through xib:

 

======= RowView. m and RowView. h file content and xib file ======

# Import "RowView. h"
@ Implementation RowView
+ (Id) rowViewWithIcon :( NSString *) icon name :( NSString *) name
{
RowView * view = [[NSBundle mainBundle] loadNibNamed: @ "RowView" owner: nil options: nil] [0];

# If 1 // use a tag to display the control in xib

// 1. Set the icon
UIButton * iconBtn = (UIButton *) [view viewWithTag: 1];
[View. iconBtn setImage: [UIImage imageNamed: icon] forState: UIControlStateNormal]; // note: the class method cannot access member variables! Therefore, _ iconBtn cannot be used.

// 2. Set the name
UILabel * nameLabel = (UILabel *) [view viewWithTag: 2];
View. nameLabel. text = name;

# Else // drag-and-drop connection attributes and xib
// 1. Set the icon
// UIButton * iconBtn = (UIButton *) [view viewWithTag: 1];
[View. iconBtn setImage: [UIImage imageNamed: icon] forState: UIControlStateNormal];

// 2. Set the name
// UILabel * nameLabel = (UILabel *) [view viewWithTag: 2];
View. nameLabel. text = name;
# Endif


Return view;
}
@ End

----------------------------------------

# Import <UIKit/UIKit. h>
@ Interface RowView: UIView
+ (Id) rowViewWithIcon :( NSString *) icon name :( NSString *) name;
@ Property (nonatomic, weak) IBOutlet UIButton * iconBtn; // if it is not a drag-and-drop, no more
@ Property (nonatomic, weak) IBOutlet UILabel * nameLabel; // if it is not a drag-and-drop, no more
@ End

 

----------------------------------------

RowView. xib:

 

Controller usage:
-(Void) viewDidLoad
{
[Super viewDidLoad];

RowView * row = [RowView rowViewWithIcon: @ "017.png" name: @" jake "];
[Self. view addSubview: row];
}

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.