1

我有一个来自标准模板的 iPhone 实用程序应用程序,所以我有MainViewController并且FlipsideViewController它被初始化和调用controller.controller'sxib 中,我有一个UISwitch被调用pathSwitch和一个UISegmentedControl被调用locationSelector,它们是插座(并且已连接!)当我调用该showInfo:(id)sender方法时,我执行以下操作:

[编辑] 添加controller... [编辑 2] 的界面 更新界面以显示添加的属性

- (IBAction)showInfo:(id)sender {    
    ALog(@"method begin...");
    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
    DLog(@">sun path visible = %@, setting flip side controller switch", sunPathIsVisible ? @"YES" : @"NO");
    // deleted -> [controller.pathSwitch setOn:sunPathIsVisible];
    controller.sunPathIsVisible = sunPathIsVisible; // added this
    DLog(@">location mode is %d, setting flip side controller segment index to %d - 1 = %d", locationMode, locationMode, locationMode - 1);
    // deleted -> controller.locationSelector.selectedSegmentIndex = locationMode - 1;
    controller.delegate = self;
    controller.locationMode = locationMode; // added this
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    [controller release];
}

@interface FlipsideViewController : UIViewController {
    id <FlipsideViewControllerDelegate> delegate;
    int locationMode; // added this
    UISegmentedControl *locationSelector;
    BOOL sunPathIsVisible;
    UISwitch *pathSwitch;
}

@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
@property int locationMode; // added this
@property (nonatomic, retain) IBOutlet UISegmentedControl *locationSelector;
@property BOOL sunPathIsVisible; // added this
@property (nonatomic, retain) IBOutlet UISwitch *pathSwitch;;

- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;

@end

// There's also the `protocol` stuff, but I left that out here...

问题是控件不接受它们的值并且总是显示段 0 和 OFF。如果我在方法中设置它们的属性controller's viewWillAppear,它们会显示正确的设置。

我这样做的时候是controller不是满载什么的?

4

2 回答 2

2

You're probably right. The cleanest way would be to add some properties to the FlipsideViewController, and set those. Then let viewWillAppear set the actual switches using these properties.

This will also put the UI layout issues of FlipsideViewController where they belong, namely in FlipsideViewController and not in any other controller that may ever use it. (i.e. if you ever decide to not use a switch but some kind of button, you can change FlipsideViewController without having to look at other code)

edit

Some clarification. Try to add properties to FlipsideViewController with these lines at the relevant places:

BOOL switchState;
NSInteger locationMode;

@property (nonatomic,assign) BOOL switchState;
@property (nonatomic,assign) NSInteger locationMode;

@synthesize switchState;
@synthesize locationMode;

Then, in your current -(IBAction)showInfo:(id)sender you could say:

FlipsideViewController *controller = [[FlipsideViewController alloc]
                                         initWithNibName:@"FlipsideView" bundle:nil];
controller.switchState = sunPathIsVisible;
controller.locationMode = locationMode;
controller.delegate = self;
// etc etc

Then, in FlipsideViewController, in viewDidLoad, put the actual handling of the switch value:

[self.pathSwitch setOn:self.sunPathIsVisible];
[self.locationSelector setSelectedSegmentIndex:self.locationMode];

This will a) solve your problem and b) separate your concerns regarding the user interface. If you would decide to change the layout of FlipsideViewController, there is no need to change any code other than that of FlipsideViewController.

There are other ways of achieving this, e.g. by letting your viewDidLoad fetch the value from its delegate, which would look like:

[self.pathSwitch setOn:[delegate pathSwitch]];

Which may be better, depending on your situation. Generally speaking I would always prefer this last approach, since it prevents synchronisation issues between your different view controllers.

于 2011-03-16T15:44:16.477 回答
1

I am not sure but shouldn't this be

@property (nonatomic, retain) IBOutlet UISegmentedControl *locationSelector;
@property (nonatomic, retain) IBOutlet UISwitch *pathSwitch;

instead of

@property (nonatomic, assign) IBOutlet UISegmentedControl *locationSelector;
@property (nonatomic, assign) IBOutlet UISwitch *pathSwitch;;
于 2011-03-17T04:24:27.333 回答