拥有数据却不知道用户界面,类似的也有table金沙js娱乐场官方网站: footer 和section

Keeps the user interface and the model objects in sync.,却不知道model对象,Keeps the user interface and the model objects in sync.,却不知道model对象,xib文件一般用来为view,view 有table header 和section header,另外你可以装配你的view controller 在你的storyboard,一个storyboard 非常像一个XIB除了他允许你lay out view controller,另外你可以装配你的view controller 在你的storyboard,controller 的root view controller.

金沙js娱乐场官方网站 19

// Notice that this is a class method and prefixed with a + instead of a

+ (instancetype)sharedStore;

@end

When this message is sent to the BNRItemStore class, the class will
check to see if the single instance of BNRItemStore has already been
created. If it has, the class will return the instance. If not, it will
create the instance and return it.

当这个消息发送给BNRItemStore,这个类将会检查是否已经创建了一个实例,如果创建了,就返回这个实例,如果没有,就创建并返回一个实例。

 

@implementation BNRItemStore

+ (instancetype)sharedStore

{
static BNRItemStore *sharedStore = nil;

// Do I need to create a sharedStore?

if (!sharedStore) {

sharedStore = [[self alloc] initPrivate]; }

return sharedStore; }

 

// If a programmer calls [[BNRItemStore alloc] init], let him know
the error of his ways

– (instancetype)init
{

@throw [NSException exceptionWithName:@”Singleton” reason:@”Use
+[BNRItemStore sharedStore]”

return nil; {

userInfo:nil];

 

// Here is the real (secret) initializer

– (instancetype)initPrivate

{
self = [super init];

return self; }

 

Notice that the variable sharedStore is declared as static. A static
variable is not destroyed when the method is done executing. Like a
global variable, it is not kept on the stack.

注意到sharedStore声明为一个static 。当一个方法执行完成后,一个static
变量不被销毁。像全局变量,它并不保存在stack中。

@class BNRItem;

@property (nonatomic, readonly) NSArray *allItems;

– (BNRItem *)createItem;

 

See the @class directive? That tells the compiler that there is a
BNRItem class and that the compiler does not need to know this class’s
details in the current file – only that it exists.

它告诉编译器有一个这样的BNRItem类,而编译器现在不需要知道它的细节,只需要知道它存在就行了。

Using the @class directive can speed up compile times considerably
because fewer files have to be recompiled when one file changes.

如果考虑到有些时候当文件变化时,文件需要重新编译
,用@class命令可能加速编译。、

但是当我们需要知道这个类中得细节的时候,我们还是需要导入的。

@interface BNRItemStore ()

@property (nonatomic) NSMutableArray *privateItems;

@end

 

– (BNRItem *)createItem

{
BNRItem *item = [BNRItem randomItem];

[self.privateItems addObject:item];

return item; }

1.5 Implementing data source methods

-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{

    return [[[BNRItemStore sharedStore] allItems]  count];

}

 

When Apple started supporting both 32-bit and 64-bit systems, they
needed an integer type that was a 32-bit int in 32-bit applications and
a 64-bit int in 64-bit applications.

当apple 开始支持32-bit
和64-bit系统,他们需要32和64的实数在32和64位的机器里面。

Thus, NSInteger (which is signed) and NSUInteger (which is unsigned)
were born. These types are used extensively throughout Apple’s
frameworks.

因此NSInteger(有符号)和NSUInteger(无符号)就出现了。

 

1.6 UITableViewCells 

Each row of a table view is a  view.

table view 的每一行都是一个view。

These views are instances of UITableViewCell.

这些view是UITableViewCell的一个实例。

A cell itself has one subview – its contentView

每一个cell 又有一个contentView。

The contentView is the superview for the content of the cell.

每个contentView是这个cell
内容的superview。金沙js娱乐场官方网站 1

The real meat of a UITableViewCell is the three subviews of the
contentView. Two of those subviews are UILabel instances that are
properties of UITableViewCell named textLabel and detailTextLabel. The
third subview is a UIImageView called imageView

实际中,UITableViewCell 是contentView的三个子类。两个是UILabel
为textLabel 和detailTextLabel。第三个是UIImageVIew
叫做imageView。金沙js娱乐场官方网站 2

Each cell also has a UITableViewCellStyle that determines which subviews
are used and their position within the contentView.

每个cell 都有一个UITableViewCellStyle决定那个subview
需要用到和在contentView
的位置。金沙js娱乐场官方网站 3

 

金沙js娱乐场官方网站 4

How do you decide which cell a BNRItem corresponds to? One of the
parameters sent to tableView:cellForRowAtIndexPath: is an NSIndexPath,
which has two properties: section and row. When this message is sent to
a data source, the table view is asking, “Can I have a cell to display
in section X, row Y?” Because there is only one section in this
exercise, your implementation will only be concerned with the row.

When this message is sent to a data source, the table view is asking,
“Can I have a cell to display in section X, row Y?”
当这个消息被发送给data source,table view
正在询问:”我能不能有一个展现在x section,y row的cell “。

1.7 Resuing UITableViewCells 

When the user scrolls the table, some cells move offscreen. Offscreen
cells are put into a pool of cells available for reuse.

当用户scroll table,一些cell 移动到offscreen。Offscreen cells
放在一个能重新利用的cell pool中。

Then, instead of creating a brand new cell for every request, the data
source first checks the pool.

不是先为每次请求创建一个新的cell,data source 首先检查这个pool

If there is an unused cell, the data source configures it with new data
and returns it to the table view.

如果里面有一个未被利用的cell,那么data source
配置给他新的数据并返回给table view。

金沙js娱乐场官方网站 5

 

There is one problem: sometimes a UITableView has different types of
cells.

有时候 UITableView有不同类型的cell。

Occasionally, you have to subclass UITableViewCell to create a special
look or behavior.

有时候你还需要继承UITableView创建不同外观的table view。

However, different subclasses floating around the pool of reusable cells
create the possibility of getting back a cell of the wrong type.

由于不同的子类产生了

You must be sure of the type of the cell returned to you so that you can
be sure of what properties and methods it has.

你必须确定返回给你的cell的类型,这样你才能确定它有什么属性和方法。

you do not care about getting any specific cell out of the pool because
you are going to change the cell content anyway.

你并不关心从pool中获得特殊的cell 因为不管怎样,你都会改变cell content。

What you need is a cell of a specific type.

你所需要的是cell 的特殊类型。

The good news is that every cell has a reuseIdentifier property of type
NSString.

好消息是每个cell 都有一个类型为NSString的reuseIdentifier属性。

When a data source asks the table view for a reusable cell, it passes a
string and says, “I need a cell with this reuse identifier.”

当一个数据源请求table view 一个reusable
cell时,它传递了一个string并说:”我需要一个cell 有这样的reuse identifier”

 

// Get a new or recycled cell UITableViewCell *cell =

[tableView dequeueReusableCellWithIdentifier:@”UITableViewCell”
forIndexPath:indexPath];

 

you are giving that control to Apple to get the benefits of the reuse
identifier.

你把控制权给Apple 来获得reuse identifier的好处 。

   

For this to work, you need to tell the table view which kind of cell it
should instantiate if there are no cells in the reuse pool.

为了让这个工作,你需要告诉table view 假若在reuse pool
里没有可用的cell时我们选择那样类型的cell 来初始化。

In BNRItemsViewController.m, override viewDidLoad to register
UITableViewCell class with the table view.

– (void)viewDidLoad

{
[super viewDidLoad];

[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@”UITableViewCell”];

}

 

Reusing cells means that you only have to create a handful of cells,
which puts fewer demands on memory.

重新利用cells意味着 你只需要创建一把cell,这将会要求很少的内存。

 

 

 

 

 

 

 

programming UITableView and
UITableViewController,iosuitableview多选 iOS programming  UITableView
and UITableViewController A UITableView displays a single column of
data…

UITableVIew
一般需要一个delegate
来通知其他涉及UITableView的对象。delegate可以是任何遵守了UITableViewDelegate
protocol 的对象。

一个NSBundle的实例在应用启动的时候就已经创建了。你可以通过发送一个消息mainBundle
给NSBundle 获取一个指向它的指针。

iOS Programming UIStoryboard 故事板,iosuistoryboard

iOS Programming UIStoryboard

In this chapter, you will use a storyboard instead. Storyboards are a
feature of iOS that allows you to instantiate and lay out all of your
view controllers in one XIB-like file. Additionally, you can wire up
view controllers in the storyboard to dictate how they get presented to
the user.

在本章中,你将使用一个storyboard。Storyboards 是iOS
的一个特色允许你初始化和放置所有你的view controllers
在一个XIB-like文件中。另外你可以装配你的view controller 在你的storyboard
来显示他们怎样显示给用户的。

The purpose of a storyboard is to minimize some of the simple code a
programmer has to write to create and set up view controllers and the
interactions between them.

一个storyboard 的目的是缩小一些simple code一个程序员必须创建和设置view
controller和在他们之间的交流。

1 Creating a Storyboard

Create a new iOS Empty Application and name it Colorboard

Then, select New File… from the New menu. Select User Interface from
the iOS section. Then, select the Storyboard template and click Next

金沙js娱乐场官方网站 6

A storyboard is a lot like a XIB, except it allows you to lay out the
relationships between view controllers in addition to laying out their
interfaces.

一个storyboard 非常像一个XIB除了他允许你lay out view controller
之间的关系除了放置他们的interfaces之外。

The Colorboard application will have a total of five view controllers,
including a UINavigationController and a UITableViewController.

金沙js娱乐场官方网站 7

Using a storyboard, you can set up the relationships shown in Figure
28.3 without writing any code.

To get started, open the utility area and the Object Library. Drag a
Navigation Controller onto the canvas. The

canvas will now look like

金沙js娱乐场官方网站 8

In addition to the UINavigationController object you asked for, the
storyboard took the liberty of creating three other objects: the view of
the navigation controller, a UITableViewController, and the view of the
UITableViewController. In addition, the UITableViewController has been
made the root view controller of the navigation controller.

除了你要求的UINavigationControler 对象,storyboard
还自由的创建了其他三个对象:view of the navigation controller,a
UITableViewController ,and  view of the
UITableViewController.另外,UITableViewController 还被设置为navigation
controller 的root view controller.

The two view controller instances are represented by the black bars on
the canvas, and their views are shown above them. You configure the view
the same as you would in a normal XIB file. To configure the view
controller itself, you select the black bar.

两个view controller 实例由在canvas 上的black bars 代表,他们的views
在他们上面显示。你配置view 与普通的XIB file 文件一样。为了配置view
controller itself,你要选择black bar.

Before you go any further, you need to tell your application about this
storyboard file. Select the Colorboard project from the project
navigator. Then, select the Colorboard target and the General tab.
Locate the Main Interface field and enter Colorboard (Figure 28.5) or
select Colorboard.storyboard from the dropdown.

在你继续之前,你需要告诉application 关于你的storyboard
file.选择哪个color board 工程,选择cjolorbard target 和general tab.

定位到main interface

 金沙js娱乐场官方网站 9

When an application has a main storyboard file, it will automatically
load that storyboard when the application launches. In addition to
loading the storyboard and its view controllers, it will also create a
window and set the initial view controller of the storyboard as the root
view controller of the window.

当一个application 有一个main storyboard file ,它将自动的加载storyboard
当启动应用时。除了加载storyboard 和它的view controllers
,它仍然创建了一个windows 和设置storyboard 的initial view controller
作为这个window的root view controller.

You can tell which view controller is the initial view controller by
looking at the canvas in the storyboard file – the initial view
controller has an arrow that fades in as it points to it.

你可以辨别哪个view controller 是initial view controller
通过查看在storyboard file的 canvas —the  initial view controller
有一个箭头 ,指向它。

Since a storyboard file supplies the window for an application, the
application delegate does not need to create a window.

因为storyboard file 提供了一个应用的窗口,因此,application delegate
不需要创建一个window。

In BNRAppDelegate.m, remove the code from
application:didFinishLaunchingWithOptions: that creates the window.

– (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

return YES;

}

2 UITableViewControllers in Storyboards

在Storyboards 里的UITableViewControllers

When using a UITableViewController, you typically implement the
appropriate data source methods to return the content of each cell. This
makes sense when you have dynamic content – like a list of items that
may change – but it is a lot of work when you have a table whose content
never changes. Storyboards allow you to add static content to a table
view without having to implement the data source methods.

当你用UITableViewController,你一般的实现恰当的data source methods
返回每个cell 的内容。这变得很有意义当你有动态的内容时,但是有许多工作
你有一个table 它的内容从来都不变。 storyboards 允许你添加Static  content
到一个table view controller 不去实现data  source methods.

To see how easy this is, you are going to add a UITableViewController to
the storyboard and give it static content.

If there is already a second view controller in your Storyboard next to
the navigation controller, select its black bar (the representation of
the view controller itself), and delete it.

Next, drag a UITableViewController from the library onto the canvas. To
set this table view controller as the root view controller of the
navigation controller, Control-drag from the navigation controller’s
view to the table view controller’s view. Let go, and from the black
panel that appears, select root view (Figure 28.7).

拖动一个UITableViewController到canvas 上 。control -drag 从navigation
controller ‘s view 到table view controller的view。显示black panel
,选择root view .

Remember that despite dragging between views, these properties are being
set on the view controllers themselves.

记住尽管是在两个view 中的dragging ,这些属性是在两个view controller
自己上设置的。

In the middle of this arrow is an icon that represents the type of
relationship between the two view controllers

中间的arrow 是一个icon代表了连个view controller 关系的类型。

Next, select the Table View of the UITableViewController. In the
attributes inspector, change the Content pop-up menu to Static Cells

紧接着,选择UITableViewController的Table view。在attributes inspector
,改变content pop-up menu 为static cells.

Back on the canvas, the selected cell will now say Title. Double-click
on the text and change it to Red.

Repeat the same steps for the second cell, but have the title read
Green. Let’s get rid of the third cell; select it and press Delete.

金沙js娱乐场官方网站 10

Finally, select the navigation bar – the area above the first cell. This
is present because the table view controller is embedded in a navigation
controller. In the Attributes Inspector, change its title to Colors.

3 Segues

Most iOS applications have a number of view controllers that users
navigate between. Storyboards allow you to set up these interactions as
segues without having to write code.

大部分iOS 应用有许多view controller 用户在他们之间导航。storyboards
允许你设置这些interactions 为segues 不用写任何代码。

A segue moves another view controller’s view onto the screen when
triggered and is represented by an instance of UIStoryboardSegue. Each
segue has a style, an action item, and an identifier.

一个segue 当触发时会移动到另一个view controller ‘s
view,别一个UIStoryboardSegue 实例。每个segue 有一个style,一个action
item和一个identifier.

The style of a segue determines how the view controller will be
presented, such as pushed onto the stack or presented modally. The
action item is the view object in the storyboard file that triggers the
segue, like a button, a bar button item, or another UIControl. The
identifier is used to programmatically access the segue. This is useful
when you want to trigger a segue that does not come from an action item,
like a shake or some other interface element that cannot be set up in
the storyboard file.

一个segue 的style 决定了view controller 将如何被展现,例如pushed onto
到stack或者presented modally.The action  item 是view object 在storyboard
file中触发了segue,比如一个button,a bar button item ,或者是UIControl.The
Identifier在通过编程获取segue时很有用。当你想触发一个segue
不是通过一个action item ,像shake 或者其他的interface element
不能在storyboard file 中设置。

A push segue pushes a view controller onto the stack of a navigation
controller.

一个push segue  把一个view controller 推送到navigation controller
的栈上。

You will need to set up two more view controllers in your storyboard,
one whose view’s background is red, and the other, green. The segues
will be between the table view controller and these two new view
controllers. The action items will be the table view’s cells; tapping a
cell will push the appropriate view controller onto the navigation
controller’s stack.

segues 是将在table view controller 和这两个 新的view controller
之间。action items 将是table view ‘s cells.点击一个cell将推送恰当的view
controller 到navigation controller的stack上。

Drag two UIViewController objects onto the canvas. Select the View of
one of the view controllers and, in the attributes inspector, change its
background color to red. Do the same for the other view controller’s
view to set its background color to green.

Next, select the cell titled Red. Control-drag to the view controller
whose view has the red background. A black panel titled Storyboard
Segues will appear. This panel lists the possible styles for this segue.
Select Push.

Then, select the Green cell and Control-drag to the other view
controller. Your canvas should look like

Note that push segues only work if the origin of the segue is inside a
navigation controller. Fortunately, the origin of these segues is the
table view controller, which meets this requirement.

注意到push segues 只有segue 的祖先是在navigation controller
里面才工作。幸运的是,这些segues的origin是table view controller
,满足这些要求。  ???

Now let’s look at another style of segue – a Modal segue. Drag a new
UIViewController onto the canvas. Set its view’s background color to
blue. You want this segue’s action item to be a bar button item on the
table view controller’s navigation item.

现在看另一种类型的segue-Modal segue.拖动一个新的UIViewController到canvas
上。设置view 的background color 为blue。你想这个segue’s action item
是在table view controller ‘s navigation item 的一个bar button item .

Drag a Bar Button Item from the library onto the right corner of the
navigation bar at the top of the table view controller’s view.

In the attributes inspector, change its Identifier to Add. Then,
Control-drag from this bar button item to the view controller you just
dropped on the canvas. Select Modal from the black panel.

To fix this, drag a UINavigationController onto the canvas and delete
the UITableViewController

为了修复这个,拖动一个UINavigationController
,删除UITableViewController。

Delete the existing modal segue, and instead have the + item trigger a
modal segue to the navigation controller.

删除应存在的modal segue,取而代之让+item 触发一个modal segue

到navigation controller .

Now that the modal view controller is within a navigation controller, it
has a navigation bar at its top. Drag a bar button item to the right
side of this navigation bar. Within the attributes inspector, change its
Identifier to Done.

现在你的modal view controller 在一个navigation controller
内部,它有一个navigation bar 在它的顶部。拖动一个bar button
item到navigation bar 的右侧。’

You will need to write a method to dismiss the modal view controller and
then connect this method to the Done button.

你需要写一个方法dismiss  这个modal view controller
然后连接这个方法到Done button.

To write code for a view controller in a storyboard, you have to create
a subclass of UIViewController and specify in the storyboard that the
view controller is an instance of your subclass.

为了为一个在storyboard 里的view controller
写代码,你必须创建一个UIViewController 的子类,并指明在storyboard
那个view controller 是你子类的一个实例

 

Let’s create a new UIViewController subclass to see how this works.
Create a new NSObject subclass and name it BNRColorViewController.

In BNRColorViewController.h, change the superclass to be
UIViewController.

@interface BNRColorViewController : UIViewController

@end

Then in BNRColorViewController.m, implement a method to dismiss itself.

– (IBAction)dismiss:(id)sender

{
[self.presentingViewController dismissViewControllerAnimated:YES

}

completion:nil];

Open Colorboard.storyboard again. Select the black bar underneath the
modally presented (blue) view controller. (This is called the scene
dock.) In the identity inspector, change the Class to
BNRColorViewController

Now, after making sure you are zoomed in, select the Done button.
Control-drag from the button to this view controller icon and let go –
when the panel appears, select the dismiss: method

金沙js娱乐场官方网站 11

4 Enabling Color Changes

You will now extend the Colorboard application to allow the user to
choose a color and save it to a list of favorite colors.

Back in Colorboard.storyboard, add one UITextField, three UILabel
objects, and three UISlider objects to the view of
BNRColorViewController so it looks like

金沙js娱乐场官方网站 12

Let’s have the background color of BNRColorViewController’s view match
the slider values. In BNRColorViewController.m, add outlets to the text
field and three sliders in the class extension.

@interface BNRColorViewController ()
@property (nonatomic, weak) IBOutlet UITextField *textField; @property
(nonatomic, weak) IBOutlet UISlider *redSlider;

@property (nonatomic, weak) IBOutlet UISlider *greenSlider; @property
(nonatomic, weak) IBOutlet UISlider *blueSlider;

@end

@implementation

All three sliders will trigger the same method when their value changes.
Implement this method in BNRColorViewController.m.

 

– (IBAction)changeColor:(id)sender

{
float red = self.redSlider.value;
float green = self.greenSlider.value;
float blue = self.blueSlider.value;
UIColor *newColor = [UIColor colorWithRed:red

green:green blue:blue

alpha:1.0]; self.view.backgroundColor = newColor;

}

Now open Colorboard.storyboard and connect the outlets from Color View
Controller (the first icon in the scene dock below the
BNRColorViewController view) to the text field and three sliders. Then
Control-drag from each slider to the Color View Controller and connect
each to the changeColor: method.

Build and run the application. Moving the sliders will cause the view’s
background color to match.

5 Passing Data Around

Prototype cells allow you to configure the various cells you will want
to return in the data source methods and assign a reuse identifier to
each one.

Prototype cells 允许你配置各种cells你想返回在data source methods
并给每一个分配reuse identifier.

Then select the UITableViewCell and set its reuse identifier to
UITableViewCell

金沙js娱乐场官方网站 13

In order to supply this table view controller with data for its table
view, you will need to create a new UITableViewController subclass.
Create a new NSObject subclass named BNRPaletteViewController.

In BNRPaletteViewController.h, change the superclass to be
UITableViewController.

@interface BNRPaletteViewController : UITableViewController

In BNRPaletteViewController.m, import BNRColorViewController.h and add
an NSMutableArray to the class extension.

#import “BNRPaletteViewController.h”

#import “BNRColorViewController.h”

@interface BNRPaletteViewController ()

@property (nonatomic) NSMutableArray *colors;

@end

 

@implementation BNRPaletteViewController
Next, implement viewWillAppear: and the table view data source methods
in

BNRPaletteViewController.m.

– (void)viewWillAppear:(BOOL)animated

{
[super viewWillAppear:animated];

[self.tableView reloadData]; }

– (NSInteger)tableView:(UITableView *)tableView

numberOfRowsInSection:(NSInteger)section

{
return [self.colors count];

}

– (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
UITableViewCell *cell =

[tableView dequeueReusableCellWithIdentifier:@”UITableViewCell”
forIndexPath:indexPath];

return cell; }

 

– (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
UITableViewCell *cell =

[tableView dequeueReusableCellWithIdentifier:@”UITableViewCell”
forIndexPath:indexPath];

return cell; }

 

Next, create a new NSObject subclass named BNRColorDescription that will
represent a user-defined color.

In BNRColorDescription.h, add properties for a UIColor and a name.

@interface BNRColorDescription : NSObject

@property (nonatomic) UIColor *color;

@property (nonatomic, copy) NSString *name;

@end

Then, in BNRColorDescription.m, override init to set default values for
these properties. @implementation BNRColorDescription

– (instancetype)init

{
self = [super init]; if (self) {

_color = [UIColor colorWithRed:0 green:0

blue:1 alpha:1];

_name = @”Blue”; }

return self; }

@end

At the top of BNRPaletteViewController.m, import BNRColorDescription.h.
Then override the colors accessor to lazily instantiate the array and
add a new BNRColorDescription to the array.

#import “BNRPaletteViewController.h” #import “BNRColorDescription.h”

@implementation BNRPaletteViewController

– (NSMutableArray *)colors

{
if (!_colors) {

_colors = [NSMutableArray array];

BNRColorDescription *cd = [[BNRColorDescription alloc] init];

[_colors addObject:cd]; }

return _colors; }

Also, update the data source method in BNRPaletteViewController.m to
display the name of the color

BNRColorDescription *color = self.colors[indexPath.row];
cell.textLabel.text = color.name;

Before we move on, open BNRColorViewController.h and add two new
properties: one that determines whether you are editing a new or
existing color, and another that indicates which color you are editing.
Do not forget to import BNRColorDescription.h at the top.

添加两个属性一个决定你是否在编辑一个新的或已经存在的color,另一个暗示哪个color
你在编辑。

#import “BNRColorDescription.h”

@interface BNRColorViewController : UIViewController

@property (nonatomic) BOOL existingColor;
@property (nonatomic) BNRColorDescription *colorDescription;

@end

Whenever a segue is triggered on a view controller, it gets sent the
message prepareForSegue:sender:.

无论什么时候segue 被触发一个view
controller,它会发送prepareForSegue:sender:.

This method gives you both the UIStoryboardSegue, which gives you
information about which segue is happening, and the sender, which is the
object that triggered the segue (a UIBarButtonItem or a UITableViewCell,
for example).

这个方法提供了两个UIStoryboardSegue,提供消息哪个segue将要发生,和sender,哪个对象触发了segue。

The segue gives you three pieces of information to use: the source view
controller (where the segue is originating from), the destination view
controller (where you are segueing to), and the identifier of the segue.
The identifier is how you can differentiate the various segues. Let’s
give your two segues useful identifiers.

segue提供给你三方面的信息:the source view controller (segue
源于哪里),destination view controller (segueing 到哪里去),the 
identifier of the segue. identifier 是你能怎样区分不同的segues 。

Open Colorboard.storyboard again. Select the modal segue and open its
attribute inspector. For the identifier, type in NewColor. Next, select
the push segue and give it the identifier ExistingColor.

With your segues identified, you can now pass your color objects around.
Open BNRPaletteViewController.m and implement prepareForSegue:sender:.

 

– (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{
if ([segue.identifier isEqualToString:@”NewColor”]) {

// If we are adding a new color, create an instance
// and add it to the colors array
BNRColorDescription *color = [[BNRColorDescription alloc] init];
[self.colors addObject:color];

// Then use the segue to set the color on the view controller
UINavigationController *nc =

(UINavigationController *)segue.destinationViewController;
BNRColorViewController *mvc =

(BNRColorViewController *)[nc topViewController];
mvc.colorDescription = color;

}
else if ([segue.identifier isEqualToString:@”ExistingColor”]) {

// For the push segue, the sender is the UITableViewCell NSIndexPath
*ip = [self.tableView indexPathForCell:sender]; BNRColorDescription
*color = self.colors[ip.row];

// Set the color, and also tell the view controller that this // is an
existing color
BNRColorViewController *cvc =

(BNRColorViewController *)segue.destinationViewController;
cvc.colorDescription = color;
cvc.existingColor = YES;

} }

Open BNRColorViewController.m and override viewWillAppear: to get rid of
the Done button if existingColor is YES.

– (void)viewWillAppear:(BOOL)animated

{
[super viewWillAppear:animated];

// Remove the ‘Done’ button if this is an existing color if
(self.existingColor) {

self.navigationItem.rightBarButtonItem = nil; }

}

Then, still in BNRColorViewController.m, override viewDidLoad to set the
initial background color, slider values, and color name.

– (void)viewDidLoad

{
[super viewDidLoad];

UIColor *color = self.colorDescription.color;

// Get the RGB values out of the UIColor object float red, green,
blue;
[color getRed:&red

green:&green blue:&blue

alpha:nil];

// Set the initial slider values self.redSlider.value = red;
self.greenSlider.value = green; self.blueSlider.value = blue;

// Set the background color and text field value
self.view.backgroundColor = color; self.textField.text =
self.colorDescription.name;

}

Finally, save the values when the view is disappearing.

– (void)viewWillDisappear:(BOOL)animated

{
[super viewWillDisappear:animated];

self.colorDescription.name = self.textField.text;

self.colorDescription.color = self.view.backgroundColor; }

Overall, storyboards make easy code easier and difficult code more
difficult.

总之,storyboards 使得容易的代码更容易,复杂的代码复杂。

Programming UIStoryboard
故事板,iosuistoryboard iOS Programming UIStoryboard In this chapter,
you will use a storyboard instead. Storyboards are a feature of iOS that
allo…

1 Creating a Storyboard

iOS programming UITableView and UITableViewController,iosuitableview多选

iOS programming  UITableView and UITableViewController

A UITableView displays a single column of data with a variable number of
rows.

UITableView 展示单列数据和不定数量的行。

   Create a new iOS Empty Application project and configure it

金沙js娱乐场官方网站 14

1.1 UITableViewController

  Model-View-Controller design pattern

  (1)Model: Holds data and knows nothing about the user interface.

拥有数据却不知道用户界面。
(2)View: Is visible to the user and knows nothing about the model
objects.

可以被用户看到,却不知道model对象
(3)Controller: Keeps the user interface and the model objects in sync.
Controls the flow of the application; for example, the controller might
be responsible for showing a “Really delete this item?” message before
actually deleting some data.

保持用户界面和模型对象同步。例如控制器可能在确定要删除一些数据之前,负责显示
“确定要删除这项吗” 。

 

a UITableView, a view object, does not handle application logic or data.

因此,UITableView,一个view object ,不知道如何处理逻辑和数据。

When using a UITableView, you must consider what else is necessary to
get the table working in your application:

当你想使用一个UITableIView时,你必须考虑在你的应用程序中还有什么需要的才能让table
工作。

(1)A UITableView typically needs a view controller to handle its
appearance on the screen.

一个UITableView 一般需要一个view controller 处理它的显示部分。

(2)A UITableView needs a data source. A UITableView asks its data
source for the number of rows

to display, the data to be shown in those rows, and other tidbits that
make a UITableView a useful user interface. Without a data source, a
table view is just an empty container. The dataSource for a UITableView
can be any type of Objective-C object as long as it conforms to the
UITableViewDataSource protocol.

UITableView需要一个data source.一个UITableView 要求data source
有几行要显示,要显示的数据,和其他好东西让UITableView 成为一个有用的user
interface .datasource 可以是任何顺从了UITableViewDataSource protocol
的对象。

(3)A UITableView typically needs a delegate that can inform other
objects of events involving the UITableView. The delegate can be any
object as long as it conforms to the UITableViewDelegate protocol.

UITableVIew 一般需要一个delegate
来通知其他涉及UITableView的对象。delegate可以是任何遵守了UITableViewDelegate
protocol 的对象。

 

An instance of the class UITableViewController can fill all three roles:
view controller, data source, and delegate.

一个UITableViewController的实例能填充三个角色:view controller ,data
source ,and delegate.

UITableViewController is a subclass of UIViewController, so a
UITableViewController has a view. A UITableViewController’s view is
always an instance of UITableView, and the UITableViewController handles
the preparation and presentation of the UITableView.

UITableViewController
是UIViewController的子类。所以UITableViewController有一个view,它的view是UITableView,UITableViewCOntroller负责UITableView的准备和展现

金沙js娱乐场官方网站 15

When a UITableViewController creates its view, the dataSource and
delegate instance variables of the UITableView are automatically set to
point at the UITableViewController

当UITableViewController创建一个view时,UITableView
的dataSource和delegate将自动的指向UITableViewController。

1.2 Subclassing UITableViewController

 

#import <UIKit/UIKit.h>

@interface BNRItemsViewController : UITableViewController

 

The designated initializer of UITableViewController is initWithStyle:,
which takes a constant that determines the style of the table view.

UITableViewController指定的初始化方法是initWithStyle,将有一个常量决定table
view的样式。

There are two options:  有两个选择:

UITableViewStylePlain and 

UITableViewStyleGrouped.

 

You are changing the designated initializer to init. As such, you need
to follow the two rules of initializers:

你将初始化部分放在了init 方法里。这样你要做两件事情。

(1)Call the superclass’s designated initializer from yours 

调用superclass 的指定initializer初始化你的。

(2)Override the superclass’s designated initializer to call yours

重现superclass的指定的initializer来调用你自己。

–  (instancetype)init
{
// Call the superclass’s designated initializer

self = [super initWithStyle:UITableViewStylePlain];

return self;

–  (instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];

 

in BNRAppDelegate ,

#import “BNRItemsViewController.h”

// Create a BNRItemsViewController BNRItemsViewController
*itemsViewController =

[[BNRItemsViewController alloc] init];

// Place BNRItemsViewController’s table view in the window hierarchy
self.window.rootViewController = itemsViewController;

1.3 UITableView’s Data Source

金沙js娱乐场官方网站 16

1.4 Creating BNRItemStore

BNRItemStore will be a singleton.

BNRItemStore将会是单例的。

 This means there will only be one instance of this type in the
application; if you try to create another instance, the class will
quietly return the existing instance

也就是说将在应用中将只有一个实例。如果你想创建另外一个实例,这个类将返回一个已经存在的实例。

#import <Foundation/Foundation.h>

@interface BNRItemStore : NSObject

实际中,UITableViewCell
是contentView的三个子类。两个是UILabel 为textLabel
和detailTextLabel。第三个是UIImageVIew
叫做imageView。金沙js娱乐场官方网站 17

notice
that you passed self as the owner of the XIB file. This ensures that
when the main NSBundle is parsing the resultant NIB file at runtime, any
connections to the File’s Owner placeholder will be made to that
BNRItemsViewController instance.

(UINavigationController

金沙js娱乐场官方网站 18

//
Change text of button to inform user of state
[sender setTitle:@”Edit” forState:UIControlStateNormal];

// If we are adding a new color, create an
instance
// and add it to the colors array
BNRColorDescription *color = [[BNRColorDescription alloc] init];
[self.colors addObject:color];

一个UITableView
一般需要一个view controller 处理它的显示部分。


(void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath

Now open
Colorboard.storyboard and connect the outlets from Color View Controller
(the first icon in the scene dock below the BNRColorViewController view)
to the text field and three sliders. Then Control-drag from each slider
to the Color View Controller and connect each to the changeColor:
method.

当这个消息发送给BNRItemStore,这个类将会检查是否已经创建了一个实例,如果创建了,就返回这个实例,如果没有,就创建并返回一个实例。

bundle =
[NSBundle mainBundle]; }

@property (nonatomic, weak) IBOutlet UISlider
*greenSlider; @property (nonatomic, weak) IBOutlet UISlider
*blueSlider;

iOS
programming  UITableView and UITableViewController

Moving a
row, however, does not require confirmation; the table view moves the
row on its own authority and reports the move to its the data source by
sending the message tableView:moveRowAtIndexPath:toIndexPath:.

cellForRowAtIndexPath:(NSIndexPath
*)indexPath

也就是说将在应用中将只有一个实例。如果你想创建另外一个实例,这个类将返回一个已经存在的实例。

 

金沙js娱乐场官方网站 19

 
 Create a new iOS Empty Application project and configure it

Notice
that this is a getter method that does more than just get.

In
addition to the UINavigationController object you asked for, the
storyboard took the liberty of creating three other objects: the view of
the navigation controller, a UITableViewController, and the view of the
UITableViewController. In addition, the UITableViewController has been
made the root view controller of the navigation controller.

// If a
programmer calls [[BNRItemStore alloc] init], let him know the error
of his ways

当tableView:commitEditingStyle:forRowAtIndexPath被发送到data
source
时,两个额外的参数也随着一起传递过来了。第一个是UITableViewCellEditingStyle,在这个情况下是UITableViewCellEditingStyleDelete,第二个是table
的行的NSIndexPath。

The
Colorboard application will have a total of five view controllers,
including a UINavigationController and a UITableViewController.

有时候你还需要继承UITableView创建不同外观的table
view。

Ultimately, it
is the dataSource of the UITableView that determines the number of rows
the table view should display. 

你需要写一个方法dismiss 
这个modal view controller 然后连接这个方法到Done button.

+
(instancetype)sharedStore

You
could toggle the editing property of UITableView directly.

现在看另一种类型的segue-Modal
segue.拖动一个新的UIViewController到canvas 上。设置view 的background
color 为blue。你想这个segue’s action item 是在table view controller ‘s
navigation item 的一个bar button item .

@property
(nonatomic) NSMutableArray *privateItems;

// Also
remove that row from the table view with an animation [tableView
deleteRowsAtIndexPaths:@[indexPath]

Prototype
cells 允许你配置各种cells你想返回在data source methods
并给每一个分配reuse identifier.

当apple
开始支持32-bit 和64-bit系统,他们需要32和64的实数在32和64位的机器里面。

[[NSBundle
mainBundle] loadNibNamed:@”HeaderView” owner:self

{
[self.presentingViewController
dismissViewControllerAnimated:YES

   

An
instance of NSBundle is created when your application launches, and you
can get a pointer to this instance by sending the message mainBundle to
NSBundle.

  • (instancetype)init

UITableViewController
is a subclass of UIViewController, so a UITableViewController has a
view. A UITableViewController’s view is always an instance of
UITableView, and the UITableViewController handles the preparation and
presentation of the UITableView.

The
method removeObjectIdenticalTo:, on the other hand, removes an object if
and only if it is the exact same object as the one passed in this
message.

The
segue gives you three pieces of information to use: the source view
controller (where the segue is originating from), the destination view
controller (where you are segueing to), and the identifier of the segue.
The identifier is how you can differentiate the various segues. Let’s
give your two segues useful identifiers.

当用户scroll
table,一些cell 移动到offscreen。Offscreen cells
放在一个能重新利用的cell pool中。

 

To see
how easy this is, you are going to add a UITableViewController to the
storyboard and give it static content.

Notice
that the variable sharedStore is declared as static. A static variable
is not destroyed when the method is done executing. Like a global
variable, it is not kept on the stack.

一旦你获取了指向main
bundle 的指针,你就可以要求它加载一个XIB文件了。

#import “BNRPaletteViewController.h”

(2)A
UITableView needs a data source. A UITableView asks its data source for
the number of rows

      

{
self = [super init]; if (self) {

In
BNRItemsViewController.m, override viewDidLoad to register
UITableViewCell class with the table view.

However,
UITableViewController also has an editing property.

{
UITableViewCell *cell =


(instancetype)initPrivate

table
view 有table header 和section header。类似的也有table footer 和section
footer。 

@end

See the
@class directive? That tells the compiler that there is a BNRItem class
and that the compiler does not need to know this class’s details in the
current file – only that it exists.

//
Remove item from array
[self.privateItems removeObjectAtIndex:fromIndex];

{
float red = self.redSlider.value;
float green = self.greenSlider.value;
float blue = self.blueSlider.value;
UIColor *newColor = [UIColor colorWithRed:red

 

 

Note
that push segues only work if the origin of the segue is inside a
navigation controller. Fortunately, the origin of these segues is the
table view controller, which meets this requirement.

(1)Call the
superclass’s designated initializer from yours 

注意到你使用了strong
属性。那是因为headerView
将是xib文件的最上层。我们使用弱引用指向属于最上层的对象。

在Storyboards
里的UITableViewControllers

The
designated initializer of UITableViewController is initWithStyle:, which
takes a constant that determines the style of the table view.

To load
a XIB file manually, you use NSBundle.

除了你要求的UINavigationControler
对象,storyboard 还自由的创建了其他三个对象:view of the navigation
controller,a UITableViewController ,and  view of the
UITableViewController.另外,UITableViewController 还被设置为navigation
controller 的root view controller.

注意到sharedStore声明为一个static
。当一个方法执行完成后,一个static
变量不被销毁。像全局变量,它并不保存在stack中。

For
example, in the Contacts app, you tap a button when you meet a new
person and want to take down all their information.

拖动一个UITableViewController到canvas
上 。control -drag 从navigation controller ‘s view 到table view
controller的view。显示black panel ,选择root view .

@property
(nonatomic, readonly) NSArray *allItems;

options:nil];

注意到push
segues 只有segue 的祖先是在navigation controller
里面才工作。幸运的是,这些segues的origin是table view controller
,满足这些要求。  ???

Thus,
NSInteger (which is signed) and NSUInteger (which is unsigned) were
born. These types are used extensively throughout Apple’s frameworks.

// Use
the default

To fix
this, drag a UINavigationController onto the canvas and delete the
UITableViewController

(2)Override
the superclass’s designated initializer to call yours

}

The
purpose of a storyboard is to minimize some of the simple code a
programmer has to write to create and set up view controllers and the
interactions between them.


 (instancetype)initWithStyle:(UITableViewStyle)style
{
return [self init];

Before
the table view will delete a row, it sends its data source a message
about the proposed deletion and waits for a confirmation message before
pulling the trigger.

With
your segues identified, you can now pass your color objects around. Open
BNRPaletteViewController.m and implement prepareForSegue:sender:.

Each
cell also has a UITableViewCellStyle that determines which subviews are
used and their position within the contentView.

在一些情况下这确实能降低你的内存。

_colors = [NSMutableArray array];

There is
one problem: sometimes a UITableView has different types of cells.

In
BNRItemStore.h, declare this method.

 

return
self; }

1.1 Editing mode 

Next,
select the cell titled Red. Control-drag to the view controller whose
view has the red background. A black panel titled Storyboard Segues will
appear. This panel lists the possible styles for this segue. Select
Push.

 

UIViewController’s
default XIB loading behavior uses the same code.

The two
view controller instances are represented by the black bars on the
canvas, and their views are shown above them. You configure the view the
same as you would in a normal XIB file. To configure the view controller
itself, you select the black bar.

@end

table
view 的cell 上的button。

}


(BNRItem *)createItem;

 

_color = [UIColor colorWithRed:0
green:0

table
view 的每一行都是一个view。

Thus,
you must add a new BNRItem to the BNRItemStore before you insert the new
row.

In the
attributes inspector, change its Identifier to Add. Then, Control-drag
from this bar button item to the view controller you just dropped on the
canvas. Select Modal from the black panel.

 

 A
UITableViewController instance automatically sets the editing property
of its table view to match its own editing property.

在你继续之前,你需要告诉application
关于你的storyboard file.选择哪个color board 工程,选择cjolorbard target
和general tab.

 
Model-View-Controller design pattern


(void)moveItemAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;

Now,
after making sure you are zoomed in, select the Done button.
Control-drag from the button to this view controller icon and let go –
when the panel appears, select the dismiss: method

 


(UIView *)headerView

You will
need to set up two more view controllers in your storyboard, one whose
view’s background is red, and the other, green. The segues will be
between the table view controller and these two new view controllers.
The action items will be the table view’s cells; tapping a cell will
push the appropriate view controller onto the navigation controller’s
stack.

Reusing
cells means that you only have to create a handful of cells, which puts
fewer demands on memory.

}

Also,
update the data source method in BNRPaletteViewController.m to display
the name of the color