从其他类的方法更改视图的属性
我正在尝试将另一个类的 MyViewController
类的对象 myImageView
的 alpha
远程调整为 0
< code>Assistant(它是 NSObject 子类)。
在 Assistant.h
#import <Foundation/Foundation.h>
@class MyViewController;
@interface Assistant : NSObject {
MyViewController *myViewController;
UIImageView *button;
}
- (void)adjustAlpha:(id)sender;
@property (nonatomic, retain) UIImageView *button;
和 Assistant.m
中,
#import "MyViewController.h"
@implementation Assistant
@synthesize button;
- (id)init
{
self = [super init];
if (self)
{
myViewController = [[MyViewController alloc] init];
NSLog(@"alloced?: %@", myViewController ? @"YES" : @"NO");
}
return self;
}
- (void)adjustAlpha:(id)sender
{
NSLog(@"method called");
myViewController.myImageView.alpha = 0;
}
该方法确实被调用,但是 myViewController.myImageView.alpha
没有改变,为什么?我需要修复哪里?感谢您的阅读 ^_^
编辑
这是 MyViewController 类:
MyViewController.h
@class Assistant;
@class MyAppAppDelegate;
@interface MyViewController : UIViewController <UITextViewDelegate, UIScrollViewDelegate>
{
UIImageView *myImageView;
Assistant *assistant;
}
@property (nonatomic, retain) UIImageView *myImageView;
MyViewController.m
#import "MyViewController.h"
#import "Assistant.h"
@implementation MyViewController
@synthesize myImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
assistant = [[Assistant alloc] init];
myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,460);
[self.view addSubview: myScrollView];
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"weather.png"]];
myImageView.frame = CGRectMake(19, 54, 48, 48);
myImageView.userInteractionEnabled = YES;
[myScrollView addSubview:myImageView];
//this button uses to call adjustAlpha
assistant.button = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button.png"]];
assistant.button.frame = CGRectMake(19, 126, 48, 48);
assistant.button.userInteractionEnabled = YES;
[myScrollView addSubview:assistant.button];
//here is how i call adjustAlpha:
assistant.adjustAlphaTap = [[UITapGestureRecognizer alloc] initWithTarget:assistant action:@selector(adjustAlpha:)];
[assistant.button addGestureRecognizer:assistant.adjustAlphaTap];
}
这个问题很复杂,需要一些实验,所以就挂在那里,等我有时间的时候就会弄清楚。对于遇到同样问题的开发人员:在单例类中声明您需要访问的对象,可以轻松地从任何其他类访问,圣诞快乐!
I'm trying to remotely adjust an object myImageView
's alpha
to 0
of MyViewController
class From Another class Assistant
(it's a NSObject subclass).
in Assistant.h
#import <Foundation/Foundation.h>
@class MyViewController;
@interface Assistant : NSObject {
MyViewController *myViewController;
UIImageView *button;
}
- (void)adjustAlpha:(id)sender;
@property (nonatomic, retain) UIImageView *button;
in Assistant.m
#import "MyViewController.h"
@implementation Assistant
@synthesize button;
- (id)init
{
self = [super init];
if (self)
{
myViewController = [[MyViewController alloc] init];
NSLog(@"alloced?: %@", myViewController ? @"YES" : @"NO");
}
return self;
}
- (void)adjustAlpha:(id)sender
{
NSLog(@"method called");
myViewController.myImageView.alpha = 0;
}
the method did get called, But myViewController.myImageView.alpha
didn't change, why? where i need to fix? thank you for reading ^_^
Edit
This is MyViewController class:
MyViewController.h
@class Assistant;
@class MyAppAppDelegate;
@interface MyViewController : UIViewController <UITextViewDelegate, UIScrollViewDelegate>
{
UIImageView *myImageView;
Assistant *assistant;
}
@property (nonatomic, retain) UIImageView *myImageView;
MyViewController.m
#import "MyViewController.h"
#import "Assistant.h"
@implementation MyViewController
@synthesize myImageView;
- (void)viewDidLoad
{
[super viewDidLoad];
assistant = [[Assistant alloc] init];
myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,460);
[self.view addSubview: myScrollView];
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"weather.png"]];
myImageView.frame = CGRectMake(19, 54, 48, 48);
myImageView.userInteractionEnabled = YES;
[myScrollView addSubview:myImageView];
//this button uses to call adjustAlpha
assistant.button = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button.png"]];
assistant.button.frame = CGRectMake(19, 126, 48, 48);
assistant.button.userInteractionEnabled = YES;
[myScrollView addSubview:assistant.button];
//here is how i call adjustAlpha:
assistant.adjustAlphaTap = [[UITapGestureRecognizer alloc] initWithTarget:assistant action:@selector(adjustAlpha:)];
[assistant.button addGestureRecognizer:assistant.adjustAlphaTap];
}
this question got complex and need some experiments, so just hanging there, will make it clear when i have time. for developer met the same problems: declare the object you need to access in a singleton class, can be easily accessed from any other classes, and Merry Christmas!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅当在
MyViewController
类的 .h 文件中将myImageView
声明为全局范围变量(也称为 iVar)并且它具有 < code>(nonatomic, keep) 与 .m 中匹配的@synthesize
。如果这听起来像您正在做的事情,那么您可以发布您的
MyViewController
h 和 m 文件的内容吗?This will only work if
myImageView
is declared as a global scope variable (also called an iVar) in the .h file of yourMyViewController
class AND that it has a property of(nonatomic, retain)
with a matching@synthesize
in the .m.If this sounds like what you're doing then can you please post the contents of your
MyViewController
h and m files.如果您已经像 Thomas 描述的那样定义了所有内容,您可以检查是否在主线程上调用了 adjustmentAlpha 方法?
If you have everything defined like Thomas described, can you check if the
adjustAlpha
method is called on the main thread?