需要 Xcode 基本解释
我实际上是 xcode 的新手。我可以自己辨认出一些东西,但对某些东西的作用以及它们为什么放在那里有疑问。我尝试阅读许多电子书,观看教程,但他们从不深入基础知识,总是只是说“添加这个,单击此处等”
有人可以给我一些问题的答案吗? 好吧,我知道 ios 应用程序主要是由视图组成的,视图由控制器控制。每个控制器都有一个头文件(.h)和一个模块?类?文件 (.m)。 .h 文件包含 .m 文件中使用的变量和函数的声明。 整个应用程序由称为“代理”的主“控制器”控制。 .h 文件中的定义可能是操作 IBAction 或 IBLabel 等。
对我来说,提出问题的是这些行:
@class FlipsideViewController;
@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end
@interface FlipsideViewController : UIViewController
@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
为什么有时在另一个视图控制器中加载委托类
@class MainViewController;
,以下内容会做什么,这意味着 @interface 声明是什么?
@interface flipAppDelegate : NSObject <UIApplicationDelegate>
很抱歉
nonatomic, retain
提出了非常愚蠢的问题,但每个教程都跳过了这些事情。 我可以遵循 YouTube 视频或手册,但它并没有教给我很多东西......
I am actually a newby at xcode. I can make out a few things be myself but have questions about what some things do and why they are put there. I have tried to read many e-books, watched tutorials, but they never go into the basics, alway just say "Add this, Click here etc"
Could someone give me some answers to a few questions please.
Ok, I know an ios app is mostly made out of Views, views are controlled by controllers. Each controller has a header (.h) file and a module?class? file (.m). The .h file contains the declarations of variables and functions used in the .m file.
The whole app is controlled by a master "controller" called the "delegate".
Definitions in .h file may be for example an action IBAction or IBLabel or something.
What raises questions for me is for example these lines:
@class FlipsideViewController;
@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end
@interface FlipsideViewController : UIViewController
@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
and why are sometimes in another view controller the delegate class loaded
@class MainViewController;
what does the following do, meaning what is the @interface declaration?
@interface flipAppDelegate : NSObject <UIApplicationDelegate>
what is
nonatomic, retain
sorry for asking really stupid questions, but every tutorial just skips these things.
I can follow a youtube video or a manual, but it doesn't teach me a lot...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我尝试一次一个地回答您的问题。
接口声明了一个类。通过声明一个类,我的意思是它指定它包含的实例变量和私有/公共方法。同样,头文件仅包含方法的声明,方法的实现/主体位于模块类中。所以,这里——
FlipsideViewController 类派生自 UIViewController/subclasses/extends。 ie 是 UIViewController 的一种,但添加了自己的功能。
同样,
子类化 NSObject 并实现 UIApplicationDelegate 协议。协议本质上是类承诺实现的一组方法(尽管可以有可选方法)。
委托模式允许一个类将其工作委托给另一个实现委托协议的类。因此,这里,
FlipsideViewController
保留委托对象的一个实例,以便可以调用它的flipsideViewControllerDidFinish:
。意味着当你为实例变量设置一个值时,该值的引用计数将增加并设置为你的变量。而且,它不会作为原子操作发生。仅在多线程环境中才需要原子性。
@synthesize 只是为变量生成 getter 和 setter 的快捷方式。
Let me try to answer your questions, one at a time.
The interface declares a class. By declaring a class, I mean it specifies the instance variables and private/public methods that it contains. Again, the header file only contains the declaration of the methods, and the implementation/body of the methods lies in the module class. So, here-
The class
FlipsideViewController
derives from/subclasses/extendsUIViewController
. i.e Is a type ofUIViewController
but adds its own features.Similarly
Subclasses NSObject and implements the
UIApplicationDelegate
protocol. A protocol is essentially a set of methods that a class promises to implement (although there can be optional methods).The delegate pattern allows a class to delegate its work to another class that implements the delegate protocol. So, here,
FlipsideViewController
keeps an instance of the delegate object, so that itsflipsideViewControllerDidFinish:
can be called.It means that when you set a value to your instance variable, the value's refcount will be incremented and set to your variable. Also, it will not happen as an atomic operation. You need atomic only in a multi-threaded environment.
@synthesize is simply a shortcut to generate getters and setters for your variables.
您确实需要阅读 Objective-C 编程语言。它非常简短,涵盖了架构、概念和语法的基础知识。
简要说明一些细节:
@class
指令用于声明类的名称,而不导入其头文件。它经常用在声明协议的.h文件中,因为协议没有实现,它不需要导入其他类(它们的.h文件)的接口。@interface
在接口文件 (.h) 中用于声明一个类,意思是描述它将具有的方法和属性、它将实现的协议以及它将继承的超类。在您的示例中,该类将被称为flipAppDelegate
,它继承了NSObject
类的所有方法和属性,并实现了UIApplicationDelegate
协议。@synthesize
在类实现文件 (.m) 中用于“合成”——即自动创建代码——您在接口 (.h) 中声明的所有属性文件。由于属性通常只需要基本访问器(仅返回当前值的“getter”和仅设置当前值的“setter”),因此使用 @synthesize 是让编译器创建的快捷方式自动为您存储值的变量、getter 方法和 setter 方法。You really need to read the Objective-C Programming Language from Apple. It's pretty brief, and runs down the basics of the architecture, concepts, and syntax.
To address, briefly, some specifics:
@class
directive is used to declare the name of a class without importing it's header file. It is often used in a .h file declaring a protocol, because a protocol has no implementation, it doesn't need to import the interfaces of other classes (their .h files).@interface
is used in an interface file (.h) to declare a class, meaning to describe the methods and properties it will have, the protocols it will implement, and the superclasses from which it will inherit. In your example, the class will be calledflipAppDelegate
, which inherits all of the methods and properties of theNSObject
class, and which implements theUIApplicationDelegate
protocol.@synthesize
is used in a class implementation file (.m) to "synthesize" --- that is, automatically create the code for --- all the properties you declared in your interface (.h) file. Since properties normally just need basic accessors (a "getter" that just returns the current value, and a "setter" that just sets the current value), using@synthesize
is a shortcut to let the compiler create the variable to store the value, the getter method, and the setter method for you automatically.我强烈建议从学习 Objective-C 开始。至少首先要有入门知识:
https://developer .apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/_index.html
这里有大量的Apple 为您可能想在开发者门户上开始的开发者提供了教程和视频。
I'd highly recommend starting by learning Objective-C. At least with a primer first:
https://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/_index.html
There's a wealth of tutorials and videos available from Apple for developers you might want to start on the developer portal.