Search code examples
iospropertiesconnectioniboutlet

accessing IBOutlet properties from another class


How do I access IBOutlets that have been created in another class?

here is my code... Class one

.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (retain, readwrite)  IBOutlet UIView *myView;

@end

.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize  myView;

- (void)viewDidLoad {
    [super viewDidLoad];
}
@end

Class two

.h

#import <UIKit/UIKit.h>

@interface ViewController2 : UIViewController
- (IBAction)accion:(id)sender;
- (IBAction)btnBack:(id)sender;
@end

.m

#import "ViewController.h"
#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (IBAction)accion:(id)sender {
    ViewController *a = [ViewController new];
    UIView *someView = [a myView];
    [someView setBackgroundColor:[UIColor redColor]];
}
- (IBAction)btnBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end

why not works? no change the backgroundColor when return on my view


Solution

  • I think you try to delegate pattern to set the values Viewcontroller2.h file Add protocol like this...

    #import <UIKit/UIKit.h>
    @protocol tutorialDelegate <NSObject>   //set protocol
    -(void)delegatesDescribedWithDescription;
    @end
    @interface ViewController2 : UIViewController
    @property (weak, nonatomic) id<tutorialDelegate> tutorialDelegate1;
    - (IBAction)accion:(id)sender;
    - (IBAction)btnBack:(id)sender;
    @end
    

    after the declaring protocol in viewcontroller2.m file first synthesize and call method like this..

    #import "ViewController.h"
    
    @interface ViewController2 ()
    
    @end
    
    @implementation ViewController2
    @synthesize tutorialDelegate1; // synthesize here
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    - (IBAction)accion:(id)sender {
        ViewController *a = [ViewController new];
        UIView *someView = [a myView];
        [someView setBackgroundColor:[UIColor redColor]];
    }
    - (IBAction)btnBack:(id)sender {
        // Here we tell delegate to invoke method in parent view.
        [self.tutorialDelegate1 delegatesDescribedWithDescription
         ];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    @end
    

    and finally we implement this method in view controller.m file but first set delegate in viewcotroller.h file like this...

    #import <UIKit/UIKit.h>
    #import "ViewController2.h" //import here
    @interface ViewController : UIViewController <tutorialDelegate> //set delegate
    @property (retain, readwrite)  IBOutlet UIView *myView;
    
    @end
    

    and viewcontroller.m file

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    @implementation ViewController
    @synthesize  myView;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
        -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
       {
        if ([segue.identifier isEqualToString:@"yourIdentifier"]) {
            ViewController2 *detailViewController =
                segue.destinationViewController;
             // here we set the ViewController to be delegate in
            // detailViewController
            detailViewController.tutorialDelegate1 = self; 
          }
        }
        // ViewController must implement tutorialDelegate's methods
    // because we specified that ViewController will conform to 
    // tutorialDelegate protocol
    -(void)delegatesDescribedWithDescription
    {   // here your code please
        viewTemp.backgroundColor =[UIColor redColor];
    }
    @end