0

我有一个imageView,在里面我放了一个按钮。我有两个问题:

  • 如何在不丢失功能的情况下正确添加按钮?
  • 我如何imageView在方法中引用我的动画来为视图设置动画?

编码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *grayFrame = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"expSqrGray.png"]];

    [self.view addSubview:grayFrame];

    grayFrame.frame = CGRectOffset(grayFrame.frame, 0, 0);

    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
    testButton.frame = CGRectMake(20, 20, 200, 44);
    testButton.backgroundColor = [UIColor blackColor];
    [testButton setTitle:@"Test" forState:UIControlStateNormal];
    [testButton addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];

    [grayFrame addSubview:testButton];
    //[self.view addSubview:testButton];
}
4

1 回答 1

1

我对 Objective-C 知之甚少,但您可能想要做的是创建类grayFrametestButton成员变量。这样,您的整个班级都可以访问这两个对象。

例如:

// Test.H
@interface YourClass : NSObject {
   UIImageView * grayFrame;
   UIButton * testButton;
}

然后,您可以使用另一种方法来引用其中任何一个。

- (void) initializeObjects {
   grayFrame = [ [ UIImageView alloc ] initWithImage:[ UIImage imageNamed:@"expSqrGray.png" ] ];
   testButton = [ UIButton buttonWithType:UIButtonTypeCustom ];

   // Do things here.
}
于 2012-09-24T04:39:22.280 回答