首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从其他类的方法更改视图的属性

从其他类的方法更改视图的属性
EN

Stack Overflow用户
提问于 2010-12-20 12:53:35
回答 2查看 198关注 0票数 0

我正在尝试将对象myImageViewalpha远程调整为0 of MyViewController类,从另一个类Assistant(它是NSObject子类)。

Assistant.h

代码语言:javascript
复制
#import <Foundation/Foundation.h>

@class MyViewController;

@interface Assistant : NSObject {

    MyViewController *myViewController;
    UIImageView *button;

}
- (void)adjustAlpha:(id)sender;

@property (nonatomic, retain) UIImageView *button;

Assistant.m

代码语言:javascript
复制
#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

代码语言:javascript
复制
@class Assistant;

@class MyAppAppDelegate;

@interface MyViewController : UIViewController <UITextViewDelegate, UIScrollViewDelegate> 
{
   UIImageView *myImageView;
   Assistant *assistant;
}
@property (nonatomic, retain) UIImageView *myImageView;

MyViewController.m

代码语言:javascript
复制
#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];

}

这个问题很复杂,需要做一些实验,所以只要挂在上面,我有时间就能弄清楚。对于开发人员来说,遇到了同样的问题:声明您需要在单例类中访问的对象,可以轻松地从任何其他类访问对象,并祝您圣诞快乐!

EN

回答 2

Stack Overflow用户

发布于 2010-12-20 12:58:10

只有当myImageView声明为MyViewController类的.h文件中的全局作用域变量(也称为iVar),并且它的属性为(nonatomic, retain),并在.m中具有匹配的@synthesize时,这才能工作。

如果这听起来像您正在做的事情,那么请您发布您的MyViewController、h和m文件的内容。

票数 1
EN

Stack Overflow用户

发布于 2010-12-20 13:02:50

如果像托马斯描述的那样定义了所有内容,那么可以检查主线程上是否调用了adjustAlpha方法吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4489787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档