0

Hey guys!

I want to change the alpha of a UIButton which sounds actually pretty simple. However, I want to change that alpha from a different class(ViewControllerB.m/.h). I got a class called "ViewController.h/.m" and from this class I call a view which appears on the ViewController like a popup. Now I got a UIButton which displays the popup, when it's touched. If the button was touched its alpha will change to "0.0" and the popup will be shown. I got the code from the popup in another class and I want to change the alpha of the button to "1.0" if the pupup was dismissed. I already got a method, that is called when the popup was dismissed. I tried everything, but it didn't worked so far. Maybe because I'm a beginner at iOS ^^.

I hope you understood what I am trying to say. I will leave the code as it is (clean) to do not confuse you with the ways I tried before.

Here you got the codes of my classes:

ViewController.h

#import <UIKit/UIKit.h>

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

- (IBAction)OpenPopUp:(id)sender;

Now in my 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

In my 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!
}

I really would appreciate anyones help and if you can please show it me on code examples.

Thank you,

Noah

NOTE: I already looked up this posts but tried unsuccsessfully.

  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

Community
  • 1
  • 1
MasterRazer
  • 1,377
  • 3
  • 16
  • 40

2 Answers2

1

There are 2 ways of doing it,

  1. In ViewController *viewDidAppear:* method check myBtn alpha. if alpha is zero then set the alpha back to 1.
  2. You can add ViewController as a delegate in ViewControllerB and ViewControllerB notifies ViewController when it is dismissed and then you set the alpha of myBtn back to 1

In my 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 

Now in 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!
}

When you are showing the popup just assign ViewController instance as a delegate of ViewControllerB instance

0

Update: I seached for it and found it. The awser from Anshul Join didn't worked for me but what worked for me was this: Update a label through button from different view

It is much less complicated and easy to implement. There are no delegates. Hope I could help anyone with this.

But summed up: Thanks to Anshul Jain for supporting me and trying to solve this for me. Thank you bro!

Community
  • 1
  • 1
MasterRazer
  • 1,377
  • 3
  • 16
  • 40