我創建了5個UIView動態,它由一個UILabel和一個UIButton組成。當我點擊按鈕時,UIView將設置爲隱藏。但它只適用於其他四個uiviews中的一個。UIView UnEven行爲當setHidden
@interface ViewController : UIViewController
{
NSMutableArray *newViews;
}
@property(nonatomic,retain)IBOutlet UILabel *welcome;
@property(nonatomic,retain)CustomView *custom;
-(void)buttonPressed:(UIButton *)sender;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *[email protected]"string of length";
int length=[name length];
newViews = [NSMutableArray array];
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"cricket", @"golf",@"wrestling", @"FootBall is good game", nil];
int yAxis=44;
int lengthOfArray=[myArray count];
for(int a=0; a<=lengthOfArray; a++){
self.custom= [[CustomView alloc]initWithFrame:CGRectMake(20, yAxis, 100, 44)];
yAxis=yAxis+50;
NSLog(@"yaxis is %i",yAxis);
self.custom.tag=200+a;
[newViews addObject:self.custom];
self.custom.Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, length+70, 30)];
self.custom.button=[[UIButton alloc]initWithFrame:CGRectMake(85,10,12,10)];
UIImage *btnImage = [UIImage imageNamed:@"button_droparrow.png"];
[self.custom.button setImage:btnImage forState:UIControlStateNormal];
[self.custom.button addTarget:self action:@selector(buttonPressed:)forControlEvents:UIControlEventTouchDown];
self.custom.button.tag=self.custom.button.tag+a;
self.custom.backgroundColor=[UIColor greenColor];
[email protected]"welcome";
custom.Label.backgroundColor = [UIColor yellowColor];
[self.custom addSubview:self.custom.button];
[self.custom addSubview:custom.Label];
[self.view addSubview:self.custom];
}
[self.custom.button addTarget:self action:@selector(buttonPressed:)forControlEvents:UIControlEventTouchDown];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)buttonPressed:(UIButton *)sender
{
[self.custom setHidden:YES];
}
@end
請幫助我。我是iOS開發新手。我需要在這裏創建具有不同引用的UIView,並且引用指定給UIButton來關閉那個特定的UIView,但我無法獲得結果。
您只隱藏'self.custom',它最終只指向您創建的最後一個'CustomView'。 – rmaddy
是的,這是問題,我如何創建每個uivew是問題,但生活的參考... –