简体   繁体   中英

how to Disable a uibutton in another class without navigating to that

My problem is different here i have a big view(classA) and a small view on it(classB), beside this small view there are four buttons which belongs to class A ,I want to disable those buttons. here is my code.

in classB.m

-(void)HideButtons{
      mainView = [[MainViewController alloc]initWithNibName:@"MainViewController"    bundle:NULL];
    [mainView HidesideBarButtons];
 }

in class B

-(void)HidesideBarButtons{

    self.peBtn.enabled = NO; 

    ByViewBtn.enabled = NO;
    favouriteBtn.enabled = NO;
    settingBtn.enabled = NO;
    ItemButton.enabled = NO;
    addVehBtn.enabled = NO;
    uploadAllBtn.enabled = NO;
    deletevideosBtn.enabled = NO;
    }

these buttons are not getting disabled.i'm not navigating to that view(cassA) i'm staying in same class;

Make an object of classA then access the button using that object

-(void)HidesideBarButtons{  
ClassA *obj =[[ClassA alloc]init];//Initialize it by your way
UIButton *myBtn=[obj getButton1];
[myBtn setEnabled:FALSE];
}

If I right understand your problem, you need to use delegate .

In ClassB.h


@protocol ClassBDelegate <NSObject>

-(void) hideSideBarButtons;

@interface ClassB: UIViewController {

__unsafe_unretained id<ClassBDelegate> delegate_;

}

@property (nonatomic, assign) id delegate;

-(void)hideButtons;

@end


----------

In ClassB.m


----------


@implementation ClassB

@synthesyze delegate = delegate_;

-(void)hideButtons {

[self.delegate hideSideBarButtons];

}


----------

In class MainViewController you need to sign up for ClassBDelegate event. Use this code in MainViewController.h


----------

@interface MainViewController : UIViewController <ClassBDelegate>
In ClassB.h

@protocol ClassBDelegate

-(void) hideSideBarButtons;

@interface ClassB: UIViewController {

__unsafe_unretained id delegate_;

}

@property (nonatomic, assign) id delegate;

-(void)hideButtons;

@end

In ClassB.m

@implementation ClassB

@synthesyze delegate = delegate_;

-(void)hideButtons {

[self.delegate hideSideBarButtons];

}

In class MainViewController you need to sign up for ClassBDelegate event. Use this code in MainViewController.h

@interface MainViewController : UIViewController

When you init ClassB in MainViewController.m you need to write:

[classB setDelegate:self];

than you need to write ClassBDelegate method - (void) hideSideBarButtons in your MainViewController.m file and disable all buttons in this method. For example ( in MainViewController.m )


-(void) hideSideBarButtons {

self.peBtn.enabled = NO;

}

Hope it helps))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM