我动态创建了5个UIView,它由一个UILabel和一个UIButton组成。当我点击按钮时,UIView将设置为隐藏。但它仅适用于其他四个uiviews。
@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 *name=@"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];
custom.Label.text=@"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,但是我无法得到结果。
答案 0 :(得分:1)
您可以使用UISegmentedControl
以及每个UIView
的xib数量。
UIView
中,您可以放置所需的UIControls
并将其链接相同。 SegmentedControl 'indexDidChangeForSegmentedControl:(UISegmentedControl *)sender'
的委托方法中删除较早的UIView
并添加所需的UIView。在主标头文件中为每个IBOutlet
UIView
@property (nonatomic, weak) IBOutlet UIView *view1;
@property (nonatomic, weak) IBOutlet UIView *view2;
在委托方法'indexDidChangeForSegmentedControl'中的.m文件中
- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender {
NSUInteger index = sender.selectedSegmentIndex;
if (UISegmentedControlNoSegment != index) {
if (currentIndex == index) {
return;
}
currentIndex = index;
switch (index) {
case 0:
{
[self.previousView removeFromSuperview];
[self.view addSubview:view1];
self.previousView = view1;
}
break;
case 1:
{
[self.previousView removeFromSuperview];
[self.view addSubview:view2];
self.previousView = view2;
}
break;
}
}
}
希望这有帮助。
答案 1 :(得分:0)
尝试这样的事情:
- (void) buttonPressed: (UIButton*) sender
{
UIView* view = sender.superview;
view.hidden = YES;
}
答案 2 :(得分:0)
如果要使用属性,则必须为每个视图创建一个属性。相反,如果要动态创建它们,可以将引用存储在数组中的每个视图中。
您应该知道/做的下一步是为每个按钮添加标签。标签只是一个数字,在这种情况下应该引用它在数组中的位置。
然后根据按钮标记(您可以从发件人检索),您可以从数组中检索正确的视图/按钮并更改其上的隐藏属性。
例如(伪代码/这不会编译):
创建视图数组
@property(非原子,强)NSMutableArray * views;
在视图中,加载创建视图
views = [[NSMutableArray alloc] init];
int nrOfViews = 5;
for(int a=0; a<=nrOfViews; a++){
UIView *view = create UIView here.
UIButton *button = create button here.
[view addSubView: button];
[button setTag: a];
[views addObject: view];
}
通过数组中保留的指针引用视图,根据按钮标记找到正确的指针。
-(void)buttonPressed:(UIButton *)sender
{
UIView *view = [views objectAtIndex: sender.tag]; //using the button tag to identify the right view.
[view setHidden: yes];
}
答案 3 :(得分:-1)
您需要进行如下更改
@property(nonatomic,strong)IBOutlet UILabel *welcome; // new arc code
@property(nonatomic,strong)UIView *custom; // new arc code
self.custom = [[UIView alloc]initWithFrame:CGRectMake(20, yAxis, 100, 44)];