Modifying UIButton's alpha property from another class

前端 未结 3 1284
轮回少年
轮回少年 2021-01-15 15:21

I\'m trying to change the alpha of an UIButton from another class. The function that is called in set the alpha property of my UIButto

相关标签:
3条回答
  • 2021-01-15 16:02

    Add a property of uiviewcontroller class in imageviewsubclass as

    ImageViewSubclass.h
    @propery (nonatomic, retain) uiviewController *parent;
    ImageViewSubclass.m
    @synthesize parent;
    

    And initialize it with "self" in view controller class when initalize object of imageviewsubclass and add on the view like

    ImageViewsubclass *oneObj = [ImageViewsubClass alloc] init];
    oneOBj.parent = self;
    

    do the same for all objects of ImageviewsubClass objects.

    and in

    - (void) tapDetected:(UITapGestureRecognizer *)tapRecognizer {
        [parent setAlphaToButton];
    }
    
    0 讨论(0)
  • 2021-01-15 16:08

    In your code, an instance of ViewController is alloced and inited, and the method setAlphaToButton is called on it. Then the view controller is released because you have no object retaining it. That's why you don't see any effect; the ViewController instance you call the method on never appears on screen.

    It's not clear how your code is supposed to work; do you have an instance of ViewController in existence when tapDetected is called? If this is the case, and this is the ViewController whose button you want to alter the alpha of, then you need to have a reference to that instance of ViewController and call setAlphaToButton on it.

    0 讨论(0)
  • 2021-01-15 16:17

    Your view is not loaded at the moment you trying to set alpha! You need to call this method after your viewDidLoad fired. You can force it by calling view, but it's kind of hackand not recommended!

    MyViewController *vc = [MyViewController new];
    vc.view; // this string will force view loading 
    [vc setAlphaToButton];
    
    0 讨论(0)
提交回复
热议问题