简体   繁体   English

初学iPhone开发问题

[英]Beginner iPhone Development Questions

I have recently started to learn about programming for the iPhone and after going through numerous online tutorials and books (most of which tell you to write this here without offering any explanation as to why or how stuff is working) I still have many questions unanswered and it would be great if someone could help me clarify them. 我最近开始学习iPhone的编程,经过大量的在线教程和书籍(大部分都告诉你在这里写这篇文章而不提供任何解释,为什么或如何工作)我还有很多问题没有答案,如果有人可以帮我澄清它们会很棒。

Here goes: 开始:

1) In Interface Builder, what is file's owner, first responder, and a delegate, and where is the actual code that draws the view? 1)在Interface Builder中,文件的所有者,第一响应者和委托是什么,绘制视图的实际代码在哪里?

2) When using Interface Builder and you add components to the screen, I understand that Interface Builder doesn't automatically write the code for you, but how should I handle the events fired by the different components? 2)当使用Interface Builder并向屏幕添加组件时,我理解Interface Builder不会自动为您编写代码,但我应该如何处理由不同组件触发的事件? From a best design practice view, should each component have its events handled in a separate file? 从最佳设计实践视图来看,每个组件是否应在单独的文件中处理其事件? (would such file be the component's delegate ?) or is it better to just make the viewcontroller class implement all of the interfaces of its components? (这样的文件是组件的委托吗?)还是让viewcontroller类实现其组件的所有接口更好?

3) When creating a UITableView for example, and I define the function: 3)例如,在创建UITableView时,我定义了该函数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [listOfItems count];
}

I am saying that the object tableView of type UITableView has this callback function. 我是说UITableView类型的对象tableView有这个回调函数。 Right? 对? So in case I have another UITableView named MyTableView I should write a function as such: 所以,如果我有另一个名为MyTableView的UITableView,我应该编写一个函数:

- (NSInteger)MyTableView:(UITableView *)MyTableView numberOfRowsInSection:(NSInteger)section {
    return [listOfItems count];
}

There's some big questions here and very hard to answer in a stack overflow post. 这里有一些很大的问题,很难在堆栈溢出帖子中回答。

1) 1)
A. What is the File Owner? A.什么是文件所有者? The nib is a file right? 笔尖是一个文件对吗? So what would own the nib file? 那么拥有nib文件的是什么? Well the owner is whatever object you call initFromNib: on. 那么所有者是你称之为initFromNib的对象:on。

AClassName *theOwner = [[AClassName alloc] initFromNib:@"nibfile"];

A nib file is just a freeze dried object, a description of an object, a serialization of an object. nib文件只是一个冻干对象,一个对象的描述,一个对象的序列化。 The object is often freeze dried with a bunch of helper objects to it can be unfrozen and ready to go. 该物体通常用一堆辅助物体冻干,它可以解冻并准备好去。 Remind me of how Egyptian Pharaohs were buried with many of their servants and many of their possessions, they would be ready to go in the after life. 让我想起埃及法老王如何与他们的许多仆人和他们的许多财产一起被埋葬,他们将准备好进入后世。 The owner is the main object that has been frozen. 所有者是已冻结的主要对象。 In a frozen state (the nib file) the owner is frozen and you can't work with it. 在冻结状态(nib文件)中,所有者被冻结,您无法使用它。 When you unfreeze by loading the nib file the main object that's unfrozen is the owner. 当您通过加载nib文件解冻时,解冻的主要对象是所有者。

B. What is the First Responder? B.什么是第一响应者? When you interact with your program by touching the screen, shaking the phone, typing on the keyboard the program must respond to that event, many other frameworks call this handling the events or actions. 当您通过触摸屏幕,摇动手机与键盘进行交互时,在键盘上键入程序必须响应该事件,许多其他框架将此称为处理事件或操作。 The First Responder is the first object that gets to respond to the user's interactions. First Responder是第一个响应用户交互的对象。 This will typically be the NSView that the user touches, which responds by redrawing itself and sending updated information to the View's Controller (Remember, NSView inherits from NSResponder - now you know why). 这通常是用户触摸的NSView,它通过重绘自身并将更新的信息发送到View的控制器来响应(记住,NSView继承自NSResponder - 现在你知道为什么)。

It's in the nib file so you can override the typical behavior. 它位于nib文件中,因此您可以覆盖典型行为。 The Cocoa Framework is used for the Mac too so programmers might want to have a single object handle keyboard input rather than letting each view handling the keyboard input itself. Cocoa框架也用于Mac,因此程序员可能希望单个对象处理键盘输入,而不是让每个视图处理键盘输入本身。 This is rarely used in iPhone programs directly, because you typically want what the user touches to respond to user interaction (redraw itself) and pass on updates. 这在iPhone程序中很少直接使用,因为您通常需要用户触摸的内容来响应用户交互(重绘自身)并传递更新。 So you can usually just ignore it in the nib file. 所以你通常可以在nib文件中忽略它。

C. What is a Delegate? C.什么是代表? What does a person do when they delegate? 一个人委派时会做什么? They tell someone else to do this job for them and report back. 他们告诉别人为他们做这份工作并报告。 People delegate all the time. 人们一直委托。 I delegate fixing my car to a car mechanic. 我委托我的汽车修理汽车修理工。 I delegate cooking dinner to the cook at a restaurant I'm dining at. 我把烹饪晚餐委托给我正在用餐的餐馆做饭。 "Johnson, I need you to write that TMI Report for me" said my boss delegating to me for I was company expert on TMI. “约翰逊,我需要你为我写这份TMI报告”我说我的老板委托我,因为我是TMI的公司专家。 Classes in the code are no different. 代码中的类没有什么不同。

The delegate in the Interface Builder is the Application's delegate. Interface Builder中的委托是Application的委托。 The UIApplication class is going to hand off lots responsibilities to it by sending messages to methods defined in the UIApplicationDelegate Protocol. UIApplication类将通过向UIApplicationDelegate Protocol中定义的方法发送消息来交付许多责任。 For instance if your delegate implements the applicationDidFinishLaunching: method it'll receive a message after the instance of UIApplication has initialized and finished its startup routine. 例如,如果您的委托实现applicationDidFinishLaunching:方法,它将在UIApplication实例初始化并完成其启动例程后收到消息。

D. Where is the drawing code? D.绘图代码在哪里? Apple has provided with the Framework in classes like NSView, NSWindow, NSTableView and it's not open-source so you can't view the source code. Apple在NSView,NSWindow,NSTableView等类中提供了Framework,它不是开源的,因此您无法查看源代码。 But the reason the window launches and appears when your first run an application built on one of Apple's templates before adding your own code is due to what occurs in the file main.m . 但是,当您在添加自己的代码之前首次运行基于Apple模板之一的应用程序时,窗口启动并出现的原因是由于main.m文件中发生的情况。

int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

The call UIApplicationMain(argc, argc, nil, nil) starts everything rolling. 调用UIApplicationMain(argc,argc,nil,nil)启动所有内容。 It loads in the nib file to unfreeze the UIApplication object and other objects in the nib file. 它加载到nib文件中以解冻UIApplication对象和nib文件中的其他对象。 Then it asks the application to start it's main loop. 然后它要求应用程序启动它的主循环。 Once unfrozen the UIApplication object (the nib's owner) then tells it's MainWindow to display on the iPhone Screen and keeps the UIApplicationDelegate in the loop about what's going on. 一旦解冻UIApplication对象(nib的所有者),然后告诉它的MainWindow显示在iPhone屏幕上,并将UIApplicationDelegate保持在循环中以了解正在发生的事情。

Well that's my answer to 1). 那是我对1)的回答。 Took a while to write I hope it helps. 花了一段时间写我希望它有所帮助。 One thing that really helped my understanding was: creating a new Window-based Application project, deleting the MainWindow.nib and then attempting to recreate it from an empty nib file, so that it functions the same way. 真正有助于我理解的一件事是:创建一个新的基于Window的应用程序项目,删除MainWindow.nib,然后尝试从空的nib文件重新创建它,以便它以相同的方式运行。

1.) File owner is the (generally) UIViewController subclass associated with the View you are building in IB (what you call GUI builder is actually termed Interface Builder) 1.)文件所有者是与您在IB中构建的View关联的(通常)UIViewController子类(您称之为GUI构建器的实际上称为Interface Builder)

First Responder is generally used in the background by the program to indicate what has control at the moment (generally don't do much with this) 第一响应者通常在程序的后台使用,以指示此刻控制的内容(通常对此没有太大作用)

The delegate is the file that receives the actions done on the view (and thus must implement how to handle these), (see below) 委托是接收对视图执行的操作的文件(因此必须实现如何处理这些操作),(见下文)

2.) Generally you will have a ViewController code file for each Interface Builder View (the File Owner). 2.)通常,每个Interface Builder视图(文件所有者)都有一个ViewController代码文件。 Generally this will be where the actions are handled for specific components (say, clicking on a button). 通常,这将是针对特定组件处理操作的位置(例如,单击按钮)。 You will set up variables (and methods for button clicks and etc) to handle each component from IB (a textfield or button is an IBOutlet, while an action like clicking on a button is an IBAction) As far as best design, I believe you should have a delegate for each view that does all the above, but I generally just use the ViewController as the delegate and implement it there (for relatively simple views) 您将设置变量(以及按钮点击等方法)来处理IB中的每个组件(文本字段或按钮是IBOutlet,而点击按钮的操作是IBAction)就最佳设计而言,我相信你应该有每个视图的委托,完成上述所有操作,但我通常只使用ViewController作为委托并在那里实现(对于相对简单的视图)

3.) No, the parameter name (which is what tableView and MyTableView are in your examples) are used inside the function to indicate the value that you passed it. 3)不,参数名称(这是什么的tableView和MyTableView在你的例子)用于在函数内部 ,以表明你通过它的价值。 When you call this function you would call it like [myTableView numberOfRowsInSection:2]; 当你调用这个函数时,你会称之为[myTableView numberOfRowsInSection:2]; and inside the function anything you needed from "myTableView" is actually referenced by the parameter name tableView ...That is for any function, but for UITableViewDelegate methods (like the one you are referencing, it will be called automatically by the UITableViewController if it's delegate is set to the file you define this function. 在函数内部,“myTableView”所需的任何内容实际上都是由参数名称tableView引用的...这适用于任何函数,但是对于UITableViewDelegate方法(就像你引用的那样,它将由UITableViewController自动调用,如果它是delegate设置为您定义此函数的文件。

Wow looking back thats some blocks of text, my best advice would be to get Beginning iPhone Development by Mark and LaMarche , it addresses all this very well. 哇回顾那些文本块,我最好的建议是获得Mark和LaMarche的Beginning iPhone开发 ,它很好地解决了这一切。

I'd also suggest looking at some of the very basic examples in the Apple documentation to get a gist for how Interface Builder and delegates are properly used. 我还建议查看Apple文档中的一些非常基本的示例,以获得有关如何正确使用Interface Builder和委托的要点。

Edit: as a commenter pointed out, the Stanford iOS course is fantastic and where i learned the basics (along with the book above) 编辑:正如评论者指出的那样,斯坦福iOS课程非常精彩,我学习了基础知识(以及上面的书)

Here goes: 开始:

  1. In Interface Builder: 在Interface Builder中:
    • The "file's owner" is the Objective-C class to which your interface belongs. “文件所有者”是您的接口所属的Objective-C类。 Usually for a view controller this means the custom view controller subclass you're creating. 通常对于视图控制器,这意味着您正在创建的自定义视图控制器子类。
    • The "first responder" is mostly there for behind-the-scenes handling of events; “第一响应者”主要用于事件的幕后处理; it's used to figure out when your class and interface handle events, and when to pass them up the responder chain. 它用于确定您的类和接口何时处理事件,以及何时将它们传递给响应器链。
    • A "delegate" is a general term for any object which receives messages about the actions of another object. “委托”是接收有关另一个对象的动作的消息的任何对象的通用术语。 In Interface Builder, it can sometimes be used to pass actions around between objects, or can refer to the "app delegate," which is a class that exists in almost all iOS projects and receives messages about the behavior of the application itself. 在Interface Builder中,它有时可用于在对象之间传递操作,或者可以引用“app delegate”,这是几乎所有iOS项目中都存在的类,并接收有关应用程序本身行为的消息。
  2. To handle events from your GUI components, the generally accepted thing to do is to define a method with return type IBAction in your view controller implementation, then hook it up to an event from a component. 要处理来自GUI组件的事件,通常接受的事情是在视图控制器实现中定义一个返回类型为IBAction的方法,然后将其连接到组件中的事件。 Then when that event is triggered, your method is called. 然后,当触发该事件时,将调用您的方法。 It's not usually necessary to break them out into a lot of separate files unless you have a very complex structure. 除非你有一个非常复杂的结构,否则通常不需要将它们分解成许多单独的文件。
  3. Not quite. 不完全的。 If you have another table view, you can call it myTableView , but you should still hook it up to the same table view delegate and data source, and the method name doesn't change . 如果您有另一个表视图,可以将其命名为myTableView ,但仍应将其连接到同一个表视图委托和数据源,并且方法名称不会更改 Let's break the first part of this method signature down: 让我们打破这个方法签名的第一部分:
    • The (NSInteger) means this method returns an integer (NSInteger)表示此方法返回一个整数
    • The phrase tableView: is part of the method name, and shouldn't be changed. 短语tableView:是方法名称的一部分,不应更改。 A table view will always call the method tableView:numberOfRowsInSection: for the information it wants, so changing to `MyTableView* would break. 表视图将始终调用方法tableView:numberOfRowsInSection:获取它想要的信息,因此更改为`MyTableView *会中断。
    • (UITableView *) means the first argument is of type UITableView (UITableView *)表示第一个参数是UITableView类型
    • tableView means that the name of the variable inside this method for the calling table view is tableView tableView表示此方法中调用表视图的变量名称为tableView

Think about reading through the View Controller Programming Guide - it covers a lot of these concepts and links to more documents that explain delegation, table views, etc. 考虑阅读View Controller编程指南 - 它涵盖了很多这些概念,并链接到更多解释委托,表格视图等的文档。

See my answer to this question for an explanation of what "loading a xib file" means and the meaning of File's Owner and outlets. 请参阅我对此问题的回答,以解释“加载xib文件”的含义以及文件所有者和出口的含义。 It's perhaps a bit advanced for a beginner, but if you really want to know the "what" of what's going on and figure out the "why do it this way" for yourself, it's probably a good read: 这对于初学者来说可能有点先进,但是如果你真的想知道正在发生什么的“什么”,并为自己找出“为什么这样做”,这可能是一个很好的阅读:

Put a UIView into a UITableView Header 将UIView放入UITableView标头中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM