0

//RootViewViewController.h

#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;

//设置视图控制器.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。但我似乎无法让它发挥作用。谢谢你的帮助。

4

1 回答 1

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;
}

选择其中之一,你应该没问题。

于 2012-06-01T03:51:11.510 回答