Table View cell events in iOS summary of two ways to use NIB and storyboard

Source: Internet
Author: User

Since IOS6, Apple has introduced the Storyborad technology to replace the nib, so that the amount of code does not write a lot, but also relatively concise. However, from the perspective of learning, Antioch that the use of nib, although more code, but for the knowledge and principles of the point of view, I think nib writing is very good. When using Storyborad's writing, if the segue scene is more, the setting has the problem, it will cause some abnormal occurrence, increase the difficulty of debugging. The following Antioch personally tested both NIB and storyboard's two-way demo. Below the difference between the simple comparison of the next, for the need to master the user to understand.

Demo is as follows











The two ways of writing a table view are different when cell cells are selected. The following are the Antioch from the VIEWCONTROLLER.M and citiesviewcontroller.m sides, respectively.

I. Comparison of code notation

Viewcontroller.m

NIB notation
#pragma mark-Implementing a Table View Delegate method
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
Nsinteger row = [Indexpath row];

Citiesviewcontroller *citiesviewcontroller = [[Citiesviewcontroller alloc] initwithstyle:uitableviewstyleplain];
NSString *selectname = [Self.listdata objectatindex:row];
Citiesviewcontroller.listdata = [Self.dictdata objectforkey:selectname];
Citiesviewcontroller.title = Selectname;
[Self.navigationcontroller Pushviewcontroller:citiesviewcontroller Animated:yes];

}

Storyboard notation
Trigger When table View Row is selected
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender
{

if ([Segue.identifier isequaltostring:@ "Showselectedprovince"])
{
Citiesviewcontroller *citiesviewcontroller = Segue.destinationviewcontroller;
Nsinteger SelectedIndex = [[Self.tableview indexpathforselectedrow] row];
NSString *selectname = [Self.listdata objectatindex:selectedindex];
Citiesviewcontroller.listdata = [Self.dictdata objectforkey:selectname];
Citiesviewcontroller.title = Selectname;
}
}

Citiesviewcontroller.m
NIB notation
#pragma mark-Implementing a Table View Delegate method
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
Nsinteger row = [Indexpath row];

Detailviewcontroller *detailviewcontroller = [[Detailviewcontroller alloc] initwithnibname:@ "DetailViewController" Bundle:nil];

Nsdictionary *dict = [Self.listdata objectatindex:row];

Detailviewcontroller.url = [dict objectforkey:@ "url"];
NSString *name = [dict objectforkey:@ "name"];
Detailviewcontroller.title = name;
[Self.navigationcontroller Pushviewcontroller:detailviewcontroller Animated:yes];

}
Storyboard notation
Trigger When table View Row is selected
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender
{

if ([Segue.identifier isequaltostring:@ "showselectedcity"])
{
Detailviewcontroller *detailviewcontroller = Segue.destinationviewcontroller;

Nsinteger SelectedIndex = [[Self.tableview indexpathforselectedrow] row];
Nsdictionary *dict = [Self.listdata objectatindex:selectedindex];

Detailviewcontroller.url = [dict objectforkey:@ "url"];

NSString *name = [dict objectforkey:@ "name"];
Detailviewcontroller.title = name;
}
}

Two. Operation mode
Storyboard Navigation Browser, do not need code definition, directly select a Viewcontroller, and then select menu operation
Editor-->embed in--->navigation Controller

When navigating from one viewcontroller to another viewcontroller, push directly on the interface, you need to select the segue in the middle of the connection, open the generic inspector, and then specify in the identifier attribute, as Showselectedprovinces





Table View cell events in iOS summary of two ways to use NIB and storyboard

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.