从其他UIViewController更改UIViewController的背景颜色

时间:2013-09-12 19:55:12

标签: ios

我在另一个上面分配了一个UIviewController。我想从这个新的控制器改变另一个的颜色,但我不知道如何。我试过这个:

-(IBAction)sliderValueChanged:(UISlider *)sender
{
NSLog(@"slider value = %f", sender.value);

ViewController1.view.backgroundColor = [UIColor colorWithRed:(160/sender.value) green:(97/sender.value) blue:(5/sender.value) alpha:0.6];
}

并导入了ViewController1,但它出错了。如果我放self.view它会正常工作但不会ViewController1。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

看起来你实际上并没有存储你想要改变背景颜色的UIViewController的引用。尝试为您正在显示的视图控制器创建属性,然后执行以下操作:

@implementation MyPoppingController

-(void)showViewController
{
    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"StoryboardInfo" bundle:nil];        
    self.myPopController = [sb instantiateInitialViewController]; 
    self.myPopController.myPresentingViewController = self;
    [self presentPopupViewController:self.myPopController animationType:MJPopupViewAnimationFade]; 
}
@end


@interface MyPoppedController

@property(strong, nonatomic) MyPopppingController *myPresentingViewController;

@end

@implementation MyPoppedController

-(IBAction)sliderValueChanged:(UISlider *)sender
{
    self.myPresentingViewController.view.backgroundColor = [UIColor colorWithRed:(160/sender.value) green:(97/sender.value) blue:(5/sender.value) alpha:0.6];
}

@end