2014-01-30 2 views
1

저는 UILabel 하나와 UIButton 하나씩으로 구성된 5 개의 UIView를 동적으로 생성했습니다. 버튼을 클릭하면 UIView가 setHidden이됩니다. 하지만 그것은 다른 4 가지 uiviews에 대해서만 작동합니다. setHidden 일 때의 UnEven UIView 동작

@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 개발에 새로운 사람이다. differrnt 참조 및 그 특정 UIView 닫으려면 UIButton 할당 할 UIView 만들려면 여기에 필요하지만 결과를 얻을 수 없습니다. 각 UIView에 대한 XIB의 번호와 함께

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

생성 한 마지막'CustomView '만 가리키는'self.custom'이 숨어 있습니다. – rmaddy

+0

네, 문제가 생겨서 문제가되는 각각의 uivew에 대한 참조를 만드는 방법이 ... –

답변

0

속성을 사용하려면 각보기에 대해 속성을 만들어야합니다. 대신 동적으로 생성하려는 경우 배열의 각 뷰에 대한 참조를 저장할 수 있습니다.

다음은 각 버튼에 태그를 추가하는 것입니다. 태그는 단지 숫자이며,이 경우 배열의 위치를 ​​참조해야합니다.

그런 다음 단추 태그 (보낸 사람에게서 검색 할 수 있음)를 기반으로 배열에서 적절한보기/단추를 검색하고 해당 숨김 속성을 변경할 수 있습니다.

(예를 들어, 의사 코드/습관이 컴파일)

를보기 어레이를

@property를 (비 원자 강한) * NSMutableArray를보기 생성하는 단계; 보기에서

로드 버튼 태그를 기반으로 올바른 하나를 배열에 보유 된 포인터를 통해 뷰에 뷰

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

당신은 나를 도울 수 있습니까? 이제 그 뷰의 하위 뷰인 uilable과 동일한 크기 여야하는 uiview를 작성해야합니다. –

+0

@khaskheli_ayaz 이미 가지고있는 코드를 포함하여 내가 생각하기에 새로운 질문을 게시해야합니다. – ophychius

-1

당신은 약간의 변경이 필요합니다.

  • UIView에서는 필요한 UIControls를 배치하고 같은 연결할 수 있습니다.
  • 각 인덱스에 대한 SegmentedControl 'indexDidChangeForSegmentedControl:(UISegmentedControl *)sender'의 대리자 메서드에서 이전 UIView을 제거하고 필요한 UIView를 추가합니다. 메인 헤더 파일에서

대리자 방법이 도움이 '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; 
     } 
    } 
} 

희망하는 .m 파일에서 각 UIView

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

IBOutlet를 추가합니다.

0

이 같은 시도는 다음과 같이

+0

내 대답이 유용하다면 고마워요 ... –

+0

올바른 것으로 표시 할 수 있습니다. 덕분에 – stosha

+0

선생님 내가 uilabel 크기에 따라 당신이 나를 도울 수있는 지에 따라 uiview 만들 필요가 ... –

1

당신은 사용할 수 UISegmentedControl :

@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)]; 
관련 문제