2012-06-01 198 views
0

//RootViewViewController.h的UIViewController困難

#import <UIKit/UIKit.h> 
#import "SettingsViewController.h" 
#import "OneSlotViewController.h" 
#import "TwoSlotViewController.h" 
#import "BingoSlotViewController.h" 
#import "SettingsViewController.h" 

@interface RootViewViewController : UIViewController{ 

    IBOutlet UIButton *owaru; 
    OneSlotViewController *oneSlotViewController; 
    TwoSlotViewController *button2ViewController; 
    BingoSlotViewController *button3ViewController; 
    UIViewController *pushedController; 
    UIButton *hajimekara; 
    SettingsViewController *settingsVc; 


} 
@property (retain, nonatomic) UIButton *hajimekara; 
@property (strong, nonatomic) IBOutlet UIButton *owaru; 
@property (nonatomic, retain) OneSlotViewController *button1ViewController; 
@property (nonatomic, retain) TwoSlotViewController *button2ViewController; 
@property (nonatomic, retain) BingoSlotViewController *button3ViewController; 
@property (nonatomic, retain) UIViewController *pushedController; 
@property (nonatomic, retain) SettingsViewController *settingsVc; 

//RootViewViewController.m

@synthesize button1ViewController; 
@synthesize button2ViewController; 
@synthesize button3ViewController; 
@synthesize pushedController; 
@synthesize settingsVc; 


-(IBAction) startButtonPressed:(id) sender { 
    if (self.settingsVc.pushedController!=nil) { 
     NSLog(@"push"); 
     [self presentViewController:self.settingsVc.pushedController animated:YES completion:NULL]; 
    } 
} 

//SettingViewController.h

#import "OneSlotViewController.h" 
#import "TwoSlotViewController.h" 
#import "BingoSlotViewController.h" 

#import "SettingsViewController.h" 

#import "AGImagePickerController.h" 


@class RootViewViewController; 


@interface SettingsViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIPopoverControllerDelegate,UIScrollViewDelegate>{ 


    OneSlotViewController *oneSlotViewController; 
    TwoSlotViewController *button2ViewController; 
    BingoSlotViewController *button3ViewController; 
    UIViewController *pushedController; 
    RootViewViewController *mainVc; 
    UIImageView *lastPriceView; 
    CustomImagePicker *_imagePicker; 
    UINavigationController *navController; 

} 


@property (nonatomic, retain) UIPopoverController *popoverController; 
@property (nonatomic, retain) OneSlotViewController *button1ViewController; 
@property (nonatomic, retain) TwoSlotViewController *button2ViewController; 
@property (nonatomic, retain) BingoSlotViewController *button3ViewController; 
@property (nonatomic, retain) UIViewController *pushedController; 
@property (nonatomic, retain) RootViewViewController *mainVc; 
@property (strong, nonatomic) IBOutlet UIImageView *lastPriceView; 
@property (nonatomic, retain) CustomImagePicker *imagePicker; 
@property (nonatomic, retain) IBOutlet UINavigationController *navController; 

//SettingViewController.m

-(IBAction) button1Pressed:(id)sender { 
    if (self.button1ViewController==nil) { 
     button1ViewController = [[OneSlotViewController alloc] init]; 
    } 
    self.pushedController = button1ViewController; 
} 

-(IBAction) button2Pressed:(id)sender { 
    if (self.button2ViewController==nil) { 
     button2ViewController = [[TwoSlotViewController alloc] init]; 
    } 
    self.pushedController = button2ViewController; 
} 

-(IBAction) button3Pressed:(id)sender { 
    if (self.button3ViewController==nil) { 
     button3ViewController = [[BingoSlotViewController alloc] init]; 
    } 
    self.pushedController = button3ViewController; 
} 

我已經聲明有兩個控制器的實例。 只要在SettingsViewController中按下按鈕,它就會將ViewController傳遞給MainViewController的startButton。 但我似乎無法使它工作。謝謝你的幫助。

+0

有什麼問題嗎? – geon

+0

@geon已添加該問題,對此抱歉。 – Bazinga

回答

1

SettingsViewController.pushedController和MainViewController.pushedController是單獨的變量。改變一個不會影響另一個。

您有兩種選擇。您需要將SettingsViewController存儲在MainViewController中,或將MainViewController傳遞給SettingsViewController。

如果MainViewController保持對SettingsViewController的引用,那麼你可以:

-(IBAction) startButtonPressed:(id) sender { 
    if (self.settingsViewController.pushedController!=nil) { 
     NSLog(@"push"); 
     [self presentViewController:self.settingsViewController.pushedController animated:YES completion:NULL]; 
    } 
} 

如果SettingsViewController傳遞給MainViewController參考,那麼您可以:

-(IBAction) button1Pressed:(id)sender { 
    if (self.button1ViewController==nil) { 
     button1ViewController = [[ViewOneController alloc] init]; 
    } 
    self.mainViewController.pushedController = button1ViewController; 
} 

-(IBAction) button2Pressed:(id)sender { 
    if (self.button2ViewController==nil) { 
     button2ViewController = [[ViewTwoController alloc] init]; 
    } 
    self.mainViewController.pushedController = button2ViewController; 
} 

-(IBAction) button3Pressed:(id)sender { 
    if (self.button3ViewController==nil) { 
     button3ViewController = [[ViewThreeController alloc] init]; 
    } 
    self.mainViewController.pushedController = button3ViewController; 
} 

挑選的這些事情之一,你應該沒問題。

+0

我的主視圖是我的根視圖。不能實例化它 – Bazinga

+0

看看我更新的問題 – Bazinga

+0

你不需要從另一個實例化一個,你只需要連接這兩個。使用界面構建器,使用應用程序委託或其他一些要點。必須有一種方法讓你將兩者聯繫在一起。 –