0

大家好!

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

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

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

视图控制器.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

视图控制器B.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. 在视图控制器之间传递数据

  2. 在 IOS 中的视图控制器之间传递数据

  3. 从另一个类修改 UIButton 的 alpha 属性

  4. 从对象 c 中的 ClassB 更改 alpha 并启用 ClassA 的 UIButton

4

2 回答 2

1

有2种方法,

  1. ViewController * viewDidAppear: * 方法中检查myBtn alpha。如果 alpha 为零,则将 alpha 设置回 1。
  2. 您可以将 ViewController 添加ViewControllerB中的委托,并且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

视图控制器.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

视图控制器B.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 实例的代表

于 2013-10-14T20:20:46.210 回答
0

更新:我搜索并找到了它。Anshul Join 的 awser 对我不起作用,但对我有用的是:Update a label through button from different view

它的复杂性要低得多,而且易于实现。没有代表。希望我能帮助任何人。

但总结起来:感谢 Anshul Jain 对我的支持并试图为我解决这个问题。谢啦兄弟!

于 2013-10-15T17:39:16.987 回答