several questions about obj-c (delegate, viewController, simulator)
我对Objective-C有几个疑问,您能帮助我理解吗?他们来了 :
self.myView.delegate =自我; ?
Ipad和Splitview有相同的事情:为什么我们像这样显式使用委托:
uisplitviewController *svc = ...
svc.delegate = pvc.otherViewController;
现在,我了解" appDelegate",但是这个委托方法呢?
2.我多次看到使用另一个" viewController",如下所示,而不是直接"分配"" mainViewControler",为什么要使用此中间语言?
MainViewController *viewController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
self.mainViewController = viewController;
[viewController release];
3.根据来自appsmuck.com的一些教程,我看到他们使用:
"(void)loadFlipsideViewController"
但没有" loadView",那么,我们可以用控制器视图替换" View"吗?
4.最后,我可以从" iphone"模拟器切换到" ipad"模拟器,但是每次构建projet时我总是得到ipad模拟器,有没有一种方法可以始终让" iphone"模拟器默认为模拟器?
就是这样。 :) 谢谢你的帮助
保罗
首先,如果您将所有这些查询分解为单独的问题,这将使我更容易获得答案。您将很难选择最能回答所有问题的答案。
就是说,我会给#1打个针。
Cocoa(和Cocoa Touch)中的许多类型的对象都发送消息。有些内容(例如NSFetchedResultsController)会在内容更改时发送消息。某些控件(如UITableViewController)在触摸表格单元格时会发送一条消息。这些消息必须转到某处。不能仅将它们发送到"那里",否则什么也听不到。这些消息需要一个目的地。在可可中,这些消息的目的地称为"代理"。如"我指定该对象为我的委托,并接收我的消息"。
如果您在控制UITableView的viewController中,通常只需将" self"指定为委托是有意义的。就是说,实际上,UITableView先生,只要将您的消息发送给我,我会处理它们。反过来,您的viewController必须(在.h中)声明它们符合UITableViewDelegate协议,然后该协议中的必需方法必须在.m中实现。
这是可可粉中非常常见的模式。在您理解之前不要继续。
1。委托设计模式
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。创建另一个
1 2 | //Now the autorelease pool has to track this object self.mainViewController = [[[MainViewController alloc] initWithNibName:@"MainView" bundle:nil] autorelease]; |
3。
4。在XCode 4中,您需要将方案设置为正确的模拟器。
对于委托,您可以在iphone sdk中检查对委托的使用。我在那里回答很长。
1 2 3 4 5 | @property (nonatomic, retain) MyClass *obj; MyClass *tmpObj = [[MyClass alloc] init]; self.obj = tmpObj; [tmpObj release]; |
让我们看看这里发生了什么。
现在查看如果一行执行此操作会发生什么:
1 | self.obj = [[MyClass alloc] init]; |
您已经在这里获得了两次所有权,一次是通过alloc,一次是通过keep属性。因此,您需要写两次
因此,摘要是如果您具有保留属性,则编写三行代码以双重所有权进行处理。
抱歉,其他部分无济于事。