在FlipsideViewController中设置UISwitch时出现问题

时间:2011-03-16 15:36:35

标签: iphone ios properties uiviewcontroller uicontrol

我有一个来自标准模板的iPhone实用程序应用,所以我MainViewControllerFlipsideViewController被初始化并调用controller.controller's xib中我有一个{{ 1}}调用UISwitchpathSwitch调用UISegmentedControl作为出口(并挂钩!)当我调用locationSelector方法时,我会执行以下操作:

[编辑]添加showInfo:(id)sender的界面 [编辑2]更新界面以显示添加的属性

controller...

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

问题是控件不接受它们的值并始终显示段0和OFF。如果我使用@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... controller's方法设置其属性,则会显示正确的设置。

当我执行此操作时,viewWillAppear是否未完全加载?

2 个答案:

答案 0 :(得分:2)

你可能是对的。最干净的方法是向FlipsideViewController添加一些属性,然后设置它们。然后让viewWillAppear使用这些属性设置实际的开关。

这也会将FlipsideViewController的UI布局问题放在FlipsideViewController中,即FlipsideViewController,而不是任何其他可能使用它的控制器。 (即如果您决定不使用开关但使用某种按钮,则可以更改FlipsideViewController而无需查看其他代码)

修改

一些澄清。尝试在相关位置使用这些行向BOOL switchState; NSInteger locationMode; @property (nonatomic,assign) BOOL switchState; @property (nonatomic,assign) NSInteger locationMode; @synthesize switchState; @synthesize locationMode; 添加属性:

-(IBAction)showInfo:(id)sender

然后,在您当前的FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil]; controller.switchState = sunPathIsVisible; controller.locationMode = locationMode; controller.delegate = self; // etc etc 中,您可以说:

FlipsideViewController

然后,在viewDidLoad中,在[self.pathSwitch setOn:self.sunPathIsVisible]; [self.locationSelector setSelectedSegmentIndex:self.locationMode]; 中,放置切换值的实际处理:

FlipsideViewController

这将a)解决您的问题,b)分离您对用户界面的疑虑。如果您决定更改FlipsideViewController的布局,则无需更改viewDidLoad以外的任何代码。

还有其他方法可以实现这一目标,例如:让你的[self.pathSwitch setOn:[delegate pathSwitch]]; 从其委托中获取值,如下所示:

{{1}}

根据您的具体情况,这可能会更好。一般来说,我总是更喜欢这种最后一种方法,因为它可以防止不同视图控制器之间的同步问题。

答案 1 :(得分:1)

我不确定,但不应该

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

而不是

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