我正在尝试将对象myImageView的alpha远程调整为0 of MyViewController类,从另一个类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];
}这个问题很复杂,需要做一些实验,所以只要挂在上面,我有时间就能弄清楚。对于开发人员来说,遇到了同样的问题:声明您需要在单例类中访问的对象,可以轻松地从任何其他类访问对象,并祝您圣诞快乐!
发布于 2010-12-20 12:58:10
只有当myImageView声明为MyViewController类的.h文件中的全局作用域变量(也称为iVar),并且它的属性为(nonatomic, retain),并在.m中具有匹配的@synthesize时,这才能工作。
如果这听起来像您正在做的事情,那么请您发布您的MyViewController、h和m文件的内容。
发布于 2010-12-20 13:02:50
如果像托马斯描述的那样定义了所有内容,那么可以检查主线程上是否调用了adjustAlpha方法吗?
https://stackoverflow.com/questions/4489787
复制相似问题