从不同的类更改UIButton alpha

时间:2013-10-14 19:57:43

标签: iphone ios objective-c uiviewcontroller uibutton

嘿伙计们!

我想更改alpha的{​​{1}},这听起来非常简单。但是,我想从不同的类(UIButton)更改该alpha。我得到了一个名为“ViewControllerB.m/.h”的类,从这个类中我调用了一个出现在ViewController上的视图,就像一个弹出窗口。现在我得到一个ViewController.h/.m,当它被触摸时显示弹出窗口。如果触摸了该按钮,其UIButton将变为“alpha”并显示弹出窗口。我从另一个类的弹出窗口获取了代码,如果pupup被解除,我想将按钮的0.0更改为“alpha”。我已经有了一个方法,当弹出窗口被解除时调用。我尝试了一切,但到目前为止还没有奏效。也许是因为我是iOS ^^的初学者。

我希望你理解我想说的话。我将保留代码(干净),不要让你与我之前尝试的方式混淆。

在这里你得到了我的课程代码:

ViewController.h

1.0

现在在我的ViewController.m中:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
//....
    IBOutlet UIButton *myBtn;
//...
}

- (IBAction)OpenPopUp:(id)sender;

在我的ViewControllerB.h中:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//...viewDidLoad....everything else

- (IBAction)OpenPopUp:(id)sender {
 [UIView animateWithDuration: 1.0
                     animations:^{
    myBtn.alpha = 0.0;
                     }];

}

@end

ViewControllerB.m:

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface ViewControllerB : UIViewController {
//...some unneccesary outlets
}

//Displaying popup.....
- (void)presentInParentViewController:(UIViewController *)parentViewController;

@end

我真的很感激任何人的帮助,如果你能在代码示例中向我展示。

谢谢,

诺亚

注意:我已经查看了这些帖子,但尝试了不合时宜的。

  1. Passing Data between View Controllers

  2. Passing data between view controllers in IOS

  3. Modifying UIButton's alpha property from another class

  4. Change alpha and enabled UIButton of ClassA from ClassB in object c

2 个答案:

答案 0 :(得分:1)

有两种方法可以做到,

  1. ViewController * viewDidAppear: *方法中检查 myBtn alpha。如果alpha为零,则将alpha设置为1。
  2. 您可以在 ViewControllerB 中添加 ViewController 作为委托,在 ViewControllerB 中将 ViewController 添加为 ViewController 您将 myBtn 的alpha设置为1
  3. 在我的ViewControllerB.h中:

    #import <UIKit/UIKit.h>
    #import <QuartzCore/QuartzCore.h>
    
    @protocol ViewControllerBDelegate <NSObject>
       - (void)didHidePopoverController
    @end
    
    @interface ViewControllerB : UIViewController {
    //...some unneccesary outlets
    }
    @property(readwrite,weak)id <ViewControllerBDelegate>delegate;
    //Displaying popup.....
    - (void)presentInParentViewController:(UIViewController *)parentViewController;
    
    @end
    

    <强> ViewController.h

    #import <UIKit/UIKit.h>
    #import "ViewControllerB.h"
    @interface ViewController : UIViewController<ViewControllerBDelegate> {
    //....
        IBOutlet UIButton *myBtn;
    //...
    }
    
    - (IBAction)OpenPopUp:(id)sender;
    
    @end 
    

    现在在ViewController.m中:

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    //...viewDidLoad....everything else
    
    - (IBAction)OpenPopUp:(id)sender {
     [UIView animateWithDuration: 1.0
                         animations:^{
        myBtn.alpha = 0.0;
                         }];
    
    }
    
     - (void)didHidePopoverController {
     [UIView animateWithDuration: 1.0
                         animations:^{
        myBtn.alpha = 1.0;
                         }];
    
    }
    
    
    @end
    

    ViewControllerB.m:

    #import "ViewControllerB.h"
    
    @interface ViewControllerB () 
    
    @end
    
    @implementation
    
    //...ViewDidload...and more
    
    - (IBAction)close:(id)sender {
        //The close button
        [self dismissFromParentViewController];
    }
    
    - (void)dismissFromParentViewController {
        //Removes the nutrition view from the superview
    
        [self willMoveToParentViewController:nil];
    
        //Removes the view with or without animation
        if (!self.shouldAnimateOnDisappear) {
            [self.view removeFromSuperview];
            [backgroundGradientView removeFromSuperview];
            [self removeFromParentViewController];
            return;
        }
        else {
            [UIView animateWithDuration:0.4 animations:^ {
                CGRect rect = self.view.bounds;
                rect.origin.y += rect.size.height;
                self.view.frame = rect;
                backgroundGradientView.alpha = 0.0f;
            }
                             completion:^(BOOL finished) {
                                 [self.view removeFromSuperview];
                                 [backgroundGradientView removeFromSuperview];
                                 [self removeFromParentViewController];
                             }];
        }
        [self.delegate didHidePopoverController];
    //THE PLACE I WANT TO CHANGE THE ALPHA BACK!
    }
    

    当您显示弹出窗口时,只需将ViewController实例指定为ViewControllerB实例的委托

答案 1 :(得分:0)

更新:我为它进行了搜索并找到了它。来自Anshul Join的灵感不适合我,但对我有用的是:Update a label through button from different view

它不那么复杂,易于实施。没有代表。希望我能帮助任何人。

但总结一下:感谢Anshul Jain支持我并试图为我解决这个问题。谢谢兄弟!