2014-01-30 46 views
1

我創建了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,但我無法獲得結果。

+0

您只隱藏'self.custom',它最終只指向您創建的最後一個'CustomView'。 – rmaddy

+0

是的,這是問題,我如何創建每個uivew是問題,但生活的參考... –

回答

0

如果您想使用屬性,您必須爲每個視圖創建一個屬性。相反,如果您想動態創建它們,您可以將對每個視圖的引用存儲在一個數組中。

接下來你應該知道/做的是給每個按鈕添加一個標籤。標籤只是一個數字,在這種情況下應該參考它在數組中的位置。

然後基於按鈕標記(可以從發件人檢索),可以從數組中檢索正確的視圖/按鈕並更改其上的Hidden屬性。

例如(僞代碼/這不會編譯):

創建視圖陣列

@屬性(非原子,強)的NSMutableArray *圖;

在View沒有負載通過留存在數組中的指針創建到該視圖的視圖

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]; 

} 
+0

感謝它工作正常 –

+0

你能plz幫助我。現在我需要創建uiview,它必須與該視圖的子視圖的uilable大小相同。 –

+0

@khaskheli_ayaz您應該爲我的想法發佈一個新問題,包括您已經擁有的代碼。 – ophychius

-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)]; 
0

嘗試是這樣的:對每個UIView廈門國際銀行的數量沿着

- (void) buttonPressed: (UIButton*) sender 
{ 
    UIView* view = sender.superview; 
    view.hidden = YES; 
} 
+0

謝謝它工作正常。 –

+0

如果我的答案是有用的,你可以標記爲正確的。謝謝 – stosha

+0

先生,我需要根據uilabel大小創建uiview,可以幫助我解決這方面的問題... –

1

你可以使用UISegmentedControl

  • 在每個UIView您可以將所需的UIControls和鏈接相同。
  • SegmentedControl 'indexDidChangeForSegmentedControl:(UISegmentedControl *)sender'的委託方法上,在每個索引中刪除先前的UIView並添加所需的UIView。

在主頭文件爲每個UIView

@property (nonatomic, weak) IBOutlet UIView *view1; 
@property (nonatomic, weak) IBOutlet UIView *view2; 

添加IBOutlet在.m文件在委託方法 'indexDidChangeForSegmentedControl'

- (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; 
     } 
    } 
} 

希望這有助於。