gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:34:32 25 4
gpt4 key购买 nike

嘿伙计们!

我想更改 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com