http://www.uplook.cn/kbase-Index-show-view17674.html 
照着这篇文章添加.xib文件,然后又添加了label标签和按钮,可以正常显示控件,但按钮点击不了。关闭重启后控件消失了,为什么?
我用的是Xcode4.2,新建的empty project里没有.xib文件,需要手动添加。
AppDelegate.h文件
 #import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>{    UIWindow *window;
    UILabel *lab;
    
}@property (nonatomic,retain) IBOutlet UIWindow *window;@property (nonatomic,retain) IBOutlet UILabel *lab;
- (IBAction)button:(id)sender;@endAppDelegate.m文件
#import "AppDelegate.h"@implementation AppDelegate
//@synthesize view;@synthesize window;
@synthesize lab;- (IBAction)button:(id)sender {
    
    lab.text = @"Hello World!";
    

- (void)dealloc
{
    [window release];
    [lab release];
    [super dealloc];
}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor orangeColor];
    NSLog(@"我已经运行了");
    [self.window makeKeyAndVisible];
    return YES; 
}- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}@end

解决方案 »

  1.   

    大哥,你在这里做这些操作是不行的
    在创建项目的时候选择单试图的类型然后在viewController中添加你那些操作(xib画好后要链接上才可以触发事件的)在或者,只能用代码实现了
      

  2.   

    UILabel那些基本的控件必须添加在view上,不能添加到windows上,windows只能添加各种view+viewController
      

  3.   

    请不要误导人家好不好。谁给你说的window上不能添加UILable,只能添加view.
    如果把window比作是电视机屏幕,view及其它元素相当于电视上显示的人物。window是固定的,而view则是可变的。你在运行时不显示,是因为你只创建了一个window.你的xib文件并没有生效。
    你需要把- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 这个消息中的window实例的创建注释掉,不用代码来创建,而是通过xib来创建self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
      // Override point for customization after application launch.
      self.window.backgroundColor = [UIColor orangeColor];上面这些全注释掉。再试试。(确保UIWindow,UILabel 与xib已建立连接)