开发者

several questions about obj-c (delegate, viewController, simulator)

i had several questions about objective-c, could you help me understand it? Here they are :

  1. why do we use sometimes :

    self.myView.delegate = self; ?

There is the same thing with the Ipad and Splitview : why do we use the delegate explicitly like this :

uisplitviewController *svc = ...

svc.delegate = pvc.otherViewController;

For now, i understand the "appDelegate", but what about this delegate method?

2.I saw several times the use of another "viewController" as below, instead of "allocating" directly the "mainViewControler", why do we use this intermediate?

MainViewController *viewController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];

self.mainViewController = viewController;

[viewController release];

3.Following some tutorials from appsmuck.com, i saw that they use :

"(void)loadFlipsideViewController "

but no "loadView", so, can we replace the "View" with the controller view?

4.Finally, i can switch from "iphone开发者_运维百科" Simulator to "ipad" simulator, but i always get the ipad simulator each time i build the projet, is there a way to always let the "iphone" simulator to be the simulator by default?

And that's it. :) Thanks for your help

Paul


1 . Delegation Design Pattern

The delegation design pattern is a way of modifying complex objects without subclassing them. Instead of subclassing, you use the complex object as is and put any custom code for modifying the behavior of that object inside a separate object, which is referred to as the delegate object. At predefined times, the complex object then calls the methods of the delegate object to give it a chance to run its custom code.

2 . The reason for creating another viewController is for memory management. It can be done in one line but then you will be needlessly adding an object to the autorelease pool.

//Now the autorelease pool has to track this object
self.mainViewController =  [[[MainViewController alloc] initWithNibName:@"MainView" bundle:nil] autorelease];

3 . -(void)loadView is inherited from UIViewController so no you can not just change it to loadViewController because that will just create a custom method. That is exactly what (void)loadFlipsideViewController is trying to accomplish and by the name of it it should load a new view controller and display it using a flip animation.

4 . In XCode 4 you need to set the scheme to the correct simulator.


First of all, it's going to me MUCH easier to get answers if you break all these queries up into separate questions. You're going to have a hard time picking an answer that answers them all best.

That said, I'll give #1 a shot.

Many types of objects in Cocoa (and Cocoa Touch) send messages. Some, like NSFetchedResultsController send messages when their contents change. Some, like UITableViewController, send a message when a table cell is touched. These messages have to GO somewhere. They can't be sent just "out there" or nothing will ever hear them. These messages need a destination. In Cocoa, the destination for these messages is called the "delegate." As in, "I designate this object to be my delegate, and receive my messages."

If you are in a viewController that is controlling a UITableView, very often is makes sense to simply specify "self" as the delegate. That is saying, in effect, hey, Mr. UITableView, just send your messages to me, I'll handle them. In turn, your viewController must declare (in the .h) that they conform to the UITableViewDelegate protocol, and then the required methods in that protocol must be implemented in your .m.

This is a VERY common pattern in Cocoa. Don't proceed until your understand it.


For delegate you can check use of Delegate in iphone sdk. I have a long answer there.

@property (nonatomic, retain) MyClass *obj;

MyClass *tmpObj = [[MyClass alloc] init];
self.obj = tmpObj;
[tmpObj release];

Let's see what happens here. [MyClass alloc] allocates an object and you have retain count of 1. In the next line you are calling the setter which also retains. So you are increasing the retain count. Naturally you will release this property later, may be in dealloc. So now you are owner of this object 2 times. To match the first ownership via alloc you release in the 3rd line.

Now see what happens if you do this in one line:

self.obj = [[MyClass alloc] init];

You have gained ownership here two times, one through alloc and one through retain property. So you need to write [obj release] twice. Otherwise you will have a memory leak. But releasing twice is confusing and easier to create bug and you should never do this. Or you need to autorelease this in the same line which is also confusing.

So the summary is if you have retain property, then write three lines of code to handle with double ownership.

Sorry, can't help with other parts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜