2013-04-10 41 views
1

我不知道如何弄清视图控制器1中视图控制器2的滑块值的变化。我想我正确调用它,但值不传递给视图。如何使用viewcontroller 2中的滑块进行更改,以反映viewcontroller 1中的更改​​?

我把一个滑块放在nib文件中,当我改变它的值时,矩形的高度和宽度的值应该改变。

这里是我的//appdelegate.m

CGRect bounds  = [self.window bounds]; 
KaleidoTab *view = [[KaleidoTab alloc] initWithFrame:bounds]; 
view.backgroundColor = [UIColor whiteColor]; 



KaleidoTabFirstViewController *vc = [[KaleidoTabFirstViewController alloc] init]; 
[vc setView: view]; 

KaleidoTabSecondViewController *vc2 = [[KaleidoTabSecondViewController alloc] init]; 
//[vc2 setView: view]; 
vc2.vc = vc; 

self.tabBarController = [[UITabBarController alloc] init]; 
self.tabBarController.viewControllers = @[vc, vc2]; 
self.window.rootViewController = self.tabBarController; 

这里是我的//secondviewcontroller.h

#import <UIKit/UIKit.h> 
#import "KaleidoTabFirstViewController.h" 
@interface KaleidoTabSecondViewController : UIViewController { 
IBOutlet UISlider *changeSize; 
IBOutlet UILabel *label1; } 
@property KaleidoTabFirstViewController *vc; 
@property (nonatomic, retain) IBOutlet UISlider *changeSize; 
- (IBAction) changeSizeSlider:(id)sender; 

这里的//secondviewcontroller.m

- (IBAction)changeSizeSlider:(UISlider *)sender 
{ 
/// Change label to match slider's value 
label1.text = [NSString stringWithFormat:@"%g", changeSize.value]; 
CGFloat changeSizeCont = changeSize.value; 
((KaleidoTab *)vc.view).rect_width = changeSizeCont; 
((KaleidoTab *)vc.view).rect_height = changeSizeCont; 
} 

kaleidotab.m有绘制矩形的方法。

我合成的属性,一切都很好。我认为我的firstviewcontroller对象有问题。

感谢您的时间。 谢谢

+0

这非常令人费解。为什么要创建视图并将其分配给视图控制器?我会定义视图控制器使用(作为它的视图属性,在笔尖)的KaleidoTab视图。然后,使用标准的MVC实践来定义一个数据模型,该数据模型保存在滑块上操作的值。任何希望在数据模型更新时都可以更新的视图。 – 2013-04-11 00:08:21

+0

@MattMartel我相信我正确的方式,与选项卡式应用程序模板我可以让我的viewcontrollers每当我想要编程方式调用,但问题是与具有nib文件的secondviewcontroller。该值不是从滑块的cahngevalue传递到视图对象vc2。试图弄清楚,但没有成功。 – user2038249 2013-04-11 00:28:51

+0

滑块标签是否正确更新? – Matt 2013-04-11 00:42:46

回答

0

您的代码使用Java类语法而不是Objective-C语法。你会发现,如果你采用的Objective-C的OOP语法简化诊断这样的问题:

// For getting property values 
[objectName propertyName]; 

// For setting property values 
[objectName setPropertyName]; 

很多iOS的框架是如何工作取决于以下约定。尝试将您的代码更改为以下内容:

- (IBAction)changeSizeSlider:(UISlider *)sender 
    { 
    // Change label to match slider's value 
    [label1 text] = [NSString stringWithFormat:@"%g", [changeSize value]]; 

    // Var for changing size 
    CGFloat changeSizeCont = [changeSize value]; 

    // Log to the console so that we can confirm changeSizeCont is being set appropriately 
    NSLog(@"changeSizeCont = %d", changeSizeCont); 


    /* I'm not used to this current syntax, this may be what's going wrong 
    It's confusing to read, it should be clearer. 
    I understand that you are attempting to grab the 1st view controller and 
    change its view properties, I think you can drop the 
    ((KaleidoTab *)vc.view).rect_width and replace it with 
    vc.view.rect_width 
    vc.view.rect_height 
    */ 
    ((KaleidoTab *)vc.view).rect_width = changeSizeCont; 
    ((KaleidoTab *)vc.view).rect_height = changeSizeCont; 

    // Log to the console so that we can confirm the rect_width and rect_height properties are receiving the change 
    NSLog("Current values for VC's view properties:"); 
    NSLog(@"rect_width = %d", ((KaleidoTab *)vc.view).rect_width); 
    NSLog(@"rect_width = %d", ((KaleidoTab *)vc.view).rect_height); 

    } 

这应该有助于您朝正确的方向前进。您将能够在控制台上检查属性值是否正在更新。如果它们并且视图没有适当地改变,那么您可能需要查看应用程序的生命周期,并找出需要重新加载或更新视图的位置,以反映视图在屏幕上的变化。

+0

请确保您阅读了大量评论,我认为这是您可能犯错的地方。 – Matt 2013-04-11 01:11:53

+0

我很感谢你的时间。我可以在日志控制台中看到滑块的值已更改。但是当我切换到firstviewcontroller选项卡视图继续其默认值。更改后的值可能会发送到视图,但是,我认为它必须重新加载才能使新的更改生效。 – user2038249 2013-04-11 02:15:55

+0

虽然我很欣赏你的时间。 – user2038249 2013-04-11 02:16:40

相关问题