iphone - 更改来自不同类的 UIButton alpha

标签 iphone ios objective-c uiviewcontroller uibutton

嘿伙计们!

我想更改 UIButtonalpha,这听起来实际上很简单。但是,我想从不同的类(ViewControllerB.m/.h)更改该 alpha。我有一个名为“ViewController.h/.m”的类,我从这个类中调用一个像弹出窗口一样出现在 ViewController 上的 View 。现在我得到了一个 UIButton,当触摸它时它会显示弹出窗口。如果触摸按钮,其 alpha 将更改为“0.0”,并且将显示弹出窗口。我从另一个类的弹出窗口中获取了代码,如果小狗被关闭,我想将按钮的 alpha 更改为“1.0”。我已经有了一个方法,当弹出窗口被关闭时会调用该方法。我尝试了一切,但到目前为止还没有成功。也许是因为我是 iOS 初学者^^。

我希望你明白我想说的。我将保留代码原样(干净),以免您与我之前尝试过的方法混淆。

这里你得到了我的类(class)的代码:

ViewController.h

#import <UIKit/UIKit.h>

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

- (IBAction)OpenPopUp:(id)sender;

现在在我的 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;
                     }];

}

@end

在我的 ViewControllerB.h 中:

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

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

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

@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];
                         }];
    }
//THE PLACE I WANT TO CHANGE THE ALPHA BACK!
}

我真的很感谢任何人的帮助,如果可以的话请在代码示例中向我展示。

谢谢,

诺亚

注意:我已经查过这篇文章,但尝试失败。

  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

最佳答案

有两种方法可以做到这一点,

  1. ViewController *viewDidAppear:*方法中检查myBtn alpha。如果 alpha 为零,则将 alpha 设置回 1。
  2. 您可以将 ViewController 添加为 ViewControllerB 中的委托(delegate),并且 ViewControllerB 会在解除时通知 ViewController,然后您将 myBtn 的 alpha 设置回 1

在我的 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 实例的委托(delegate)

关于iphone - 更改来自不同类的 UIButton alpha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19368235/

相关文章:

iphone - 我想翻转我的应用程序的启动画面 - 我如何模仿翻页动画,就像打开书的封面一样?

ios - 如何修复 iOS 8 上横向导航栏项目的高度?

Android - 将我的 UITableView 移植到 android

iPhone:ABPeoplePickerNavigationController 将无法在 UITabBarController 中正确显示

iphone - spring mvc解码 + 正在被替换为空格

iphone - 奇怪,包括 AFNetwork 的问题?

ios - 如何在标签栏不从主视图中消失的情况下继续查看 Controller

ios - xcode 7 + 核心数据 : cannot import module being compiled

iphone - NSURLConnection 在仪器中显示为泄漏

ios - Xcode - 接收器类型 'BFTask' 例如消息是前向声明