2011-07-06 15 views
0

clearbtnを呼び出すと、スイッチはリセットされず、同じ状態のままです。また、私はpushmeを呼び出し、スイッチがオンかオフかどうかをチェックすると常にオフを検出しますが、これが原因でしょうか?これをどうすれば解決できますか?私はウィンドウベースのアプリケーションを作成しました。私はまだUIViewControllerを作成しませんでしたが、この問題の原因は何ですか?ありがとうございました。iPhone UISwitch問題

マイ.hファイル

UIButton *_pushmebutton; 
    UITextField *_nametextfield; 
    UILabel *_pushmelabel; 
    UIImageView *_bgimage; 
    UISwitch *_bgswitch; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UIButton *pushmebutton; 
@property (nonatomic, retain) IBOutlet UITextField *nametextfield; 
@property (nonatomic, retain) IBOutlet UILabel *pushmelabel; 
@property (nonatomic, retain) IBOutlet UISwitch *bgswitch; 
@property (nonatomic, retain) IBOutlet UIImageView *bgimage; 
- (IBAction)textFieldReturn:(id)sender; 
- (IBAction)backgroundTouched:(id)sender; 
- (IBAction)pushme:(id)sender; 
- (IBAction)clearbtn:(id)sender; 
- (IBAction)changebg:(id)sender; 

マイ.mファイル

#import "MyFirstAppAppDelegate.h" 

@implementation MyFirstAppAppDelegate 


@synthesize window=_window; 
@synthesize pushmebutton = _pushmebutton; 
@synthesize nametextfield = _nametextfield; 
@synthesize pushmelabel = _pushmelabel; 
@synthesize bgswitch = _bgswitch; 
@synthesize bgimage = _bgimage; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    [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:. 
    */ 
} 

- (void)dealloc 
{ 
    [_window release]; 
    [_pushmebutton release]; 
    [_pushmelabel release]; 
    [_nametextfield release]; 
    [super dealloc]; 
} 


-(IBAction)pushme:(id)sender 
{ 
    if([_nametextfield.text length] == 0) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Please Enter Your Name" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; [alert release]; 
    } 
    else 
    { 
    [_nametextfield resignFirstResponder]; 
     if(_bgimage.hidden == false) 
     { 
      self.pushmelabel.backgroundColor = [UIColor redColor]; 
     } 
     if(_bgswitch.on) 
     { 
      NSString *msg = [[NSString alloc] initWithFormat:@"Hello, %@ Thanks for using the App! Switch: On!", _nametextfield.text]; 
      self.pushmelabel.text = msg; 
     } 
     else 
     { 
     NSString *msg = [[NSString alloc] initWithFormat:@"Hello, %@ Thanks for using the App! Switch: Off!", _nametextfield.text]; 
     self.pushmelabel.text = msg; 
     } 
    } 
} 
-(IBAction)clearbtn:(id)sender; 
{ 
    _nametextfield.text = @""; 
    _pushmelabel.text = @""; 
    _pushmelabel.backgroundColor = [UIColor clearColor]; 
    [_bgswitch setOn:NO animated:YES]; 
} 
-(IBAction)textFieldReturn:(id)sender 
{ 
    [sender resignFirstResponder]; 
} 
-(IBAction)backgroundTouched:(id)sender 
{ 
    [_nametextfield resignFirstResponder]; 
} 
-(IBAction)changebg:(id)sender 
{ 
    if(_bgimage.hidden == false) 
    { 
    _bgimage.hidden = true; 
     _pushmelabel.backgroundColor = [UIColor clearColor]; 
    } 
    else if(_bgimage.hidden == true) 
    { 
     _bgimage.hidden = false; 
     if([_pushmelabel.text length] > 0) 
     { 
     _pushmelabel.backgroundColor = [UIColor redColor]; 
     } 
    } 
} 

@end 

答えて

3

私はあなたがIBのスイッチにコンセントを接続していないと言っています。その接続を確認し、必要に応じて作成してください。接続がすでにある場合は、スイッチをpushmeメソッドでNSLogし、nilに出力されているかどうかを確認してください。

0

あなたは本当にこのようApplicationDelegateを使うべきではありません - アプリケーションイベントのデリゲートであることを意図しています、それがすべてですそのはず。 UIViewControllerから継承しないので、通常のビューライフサイクルのものには関与しないので、あなたの問題は非常にそうです。あなたのビューのものをUIViewControllerに入れて、もう一度スイッチを試してみてください。

+0

ありがとうございます。 – iPhoneDev85

+0

ビューのライフサイクルとは関係ありません。このような基本的なアプリケーションの場合、それをアプリケーションデリゲートに入れても問題はありません。 – FeifanZ

+0

問題ありません。多くのデベロッパーがiOSを初めて起動するときに問題を引き起こすものの1つは、クラス階層と役割分担が実際にはガイドラインではないことです。ほとんどの場合、それは動作するために必要です。これはUIViewControllerの一例です。ウィンドウに最初に追加されたUIViewController-descendantクラスは、ランタイム(viewDidLoad、viewWilAppearなど)に 'plumbed in'されます。これは、コントローラビューが正しく動作するために必要です。 – RyanR