从另一个 class 访问 IBOutlet 属性

accessing IBOutlet properties from another class

如何访问在另一个 class 中创建的 IBOutlets?

这是我的代码... Class一个

.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两个

.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

为什么不行?当 return 在我看来

时不改变背景颜色

我认为您尝试委托模式来设置值 Viewcontroller2.h 文件 像这样添加协议...

#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

viewcontroller2.m 文件中声明协议后,首先像这样合成和调用方法..

#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

最后我们在视图 controller.m 文件中实现了这个方法,但首先在 viewcotroller.h 文件中像这样设置委托...

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

@end

viewcontroller.m文件

#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