加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 您的应用程序代理设置在哪里,并初始化其窗口和vi

发布时间:2020-12-14 20:05:59 所属栏目:百科 来源:网络整理
导读:我有一个非常新手的iOS应用程序的问题…如果我创建一个新的基于视图的应用程序称为TestForStackOverflow,Xcode自动为TestForStackOverflowAppDelegate.h创建这样的代码: @class TestForStackOverflowViewController;@interface TestForStackOverflowAppDele
我有一个非常新手的iOS应用程序的问题…如果我创建一个新的基于视图的应用程序称为TestForStackOverflow,Xcode自动为TestForStackOverflowAppDelegate.h创建这样的代码:

@class TestForStackOverflowViewController;

@interface TestForStackOverflowAppDelegate : NSObject <UIApplicationDelegate>

@property (nonatomic,retain) IBOutlet UIWindow *window;

@property (nonatomic,retain) IBOutlet TestForStackOverflowViewController *viewController;

@end

以及TestForStackOverflowAppDelegate.m中的以下内容:

#import "TestForStackOverflowAppDelegate.h"

#import "TestForStackOverflowViewController.h"

@implementation TestForStackOverflowAppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

[...]

这里来我的问题:

1)TestForStackOverflowAppDelegate类设置为当前应用程序的委托在哪里?是否“自动”完成?我看到main.m源文件只包含在内
以下代码:

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

应该不会在UIApplicationMain函数调用的第四个参数中设置应用程序委托类?

2)TestForStackOverflowAppDelegate类的窗口和viewController属性在哪里设置?

3)这可能是微不足道的,但为什么我们合成了window = _window,而在TestForStackOverflowAppDelegate接口中没有名为_window的实例变量?我已经看到你可以声明@properties,而没有相应的iVars在类接口(也许它们是由编译器自动创建的),但这是一个很好的做法,还是应该总是在你的类中创建相应的iVars?

请原谅我很长的信息,我只希望我没有写出一个太明显的问题,因为这里在意大利是深夜,我很累.但是当这些问题进入我的头脑,我迫不及待解决方案 :)

解决方法

如何加载app代表类?

在-info.plist文件中有一个名为“主要nib文件库名称”的键,并且有一个像MainWindow.xib的视图.在该xib文件中,有一个文件的所有者代理对象,它有一个委托设置为应用程序委托类.

在设计师中打开XIB.注意文件的所有者对象在顶部,上下文单击它并查看委托对象.现在看下面(XCode 4)行中的设计中的对象 – 你应该在那里看到应用程序委托对象.

appDelegate集合的窗口和viewController在哪里?

在设计器和上下文中单击(XCode 4)行下的应用程序委托对象.窗口和viewController目标被绑在那里.

_window iVar

它自动为您提供.不清楚我是否由编译器继承或生成.

_ iVars的更多内容
Why rename synthesized properties in iOS with leading underscores?

如果您选择在代码中执行等效代码:

删除plist xib参考
main.m:通过代表的名称

int main(int argc,char *argv[])
{
    int retVal = UIApplicationMain(argc,@"HelloViewAppDelegate");

应用程式委托:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    CGRect screenBounds = [[UIScreen mainScreen] applicationFrame];
    CGRect windowBounds = screenBounds;
    windowBounds.origin.y = 0.0;

    // init window
    [self setWindow: [[UIWindow alloc] initWithFrame:screenBounds]];

    // init view controller
    _mainViewController = [[MainViewController alloc] init];

    [[self window] addSubview:[_mainViewController view]];

    [self.window makeKeyAndVisible];
    return YES;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读