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

发布于 2024-10-08 10:45:06 字数 2623 浏览 0 评论 0原文

我正在尝试将另一个类的 MyViewController 类的对象 myImageViewalpha 远程调整为 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 alphato 0 of MyViewControllerclass 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃扇骨 2024-10-15 10:45:06

仅当在 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 your MyViewController 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.

邮友 2024-10-15 10:45:06

如果您已经像 Thomas 描述的那样定义了所有内容,您可以检查是否在主线程上调用了 adjustmentAlpha 方法?

If you have everything defined like Thomas described, can you check if the adjustAlpha method is called on the main thread?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文