gpt4 book ai didi

ios - 从另一个类访问 IBOutlet 属性

转载 作者:行者123 更新时间:2023-11-29 01:51:51 25 4
gpt4 key购买 nike

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

这是我的代码... 第一类

.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

第二类

.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

为什么不行?返回我的 View 时不更改背景颜色

最佳答案

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

#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中声明协议(protocol)之后文件首先合成并调用这样的方法..

#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

最后我们在 View controller.m 中实现这个方法文件但首先在 viewcotroller.h 中设置委托(delegate)像这样的文件...

#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

关于ios - 从另一个类访问 IBOutlet 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31308734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com