2017-10-10 16 views
0

UIToolbar이 있고 UISegmentedControl이 몇 개 추가되었습니다. 내 문제는 UISegmentedControl에 대한 내 selector은 IOS 11에서 호출되지 않습니다. 그러나 IOS 10 이하 버전에서는 정상적으로 작동합니다.UISegmentedControl의 선택자가 이벤트에 대해 트리거되지 않았습니다. 11

저는 UITextField입니다. 날짜 선택 도구를 입력보기로 사용하고 있으며이 도구 모음을 assesory보기로 추가했습니다. 툴바에서 완료 버튼을 클릭하면 UITextField의 값을 설정합니다.

 UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50)]; 
     [toolBar setBackgroundColor:[UIColor whiteColor]]; 

     doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:"done" ]]; 
     doneButton.momentary = YES; 
     doneButton.frame =CGRectMake(toolBar.frame.size.width - 50,5 , 30, toolBar.frame.size.height- 2 *5); 
     [doneButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; 
     doneButton.tintColor = [UIColor blackColor]; 
     [doneButton addTarget:self action:@selector(donePressed) forControlEvents:UIControlEventValueChanged]; 
     [toolBar addSubview:doneButton]; 

결코 IOS 11.0에서 호출되지!하는 donePressed 방법하지만 같은 코드가 낮은 IOS 버전에서 작동합니다.

모든 문제를 잘 알고 있습니다.

+1

donePressed() 메소드 코드 넣기 – iPatel

+0

간단합니다 - (void) donePressed {}. –

답변

0

다음과 같은 방식으로 코드를 시도했으며 iOS 11.1에서 작동합니다. 선택기 동작 donePressed가 제대로 SegmentControl

// ViewController.h 

#import <UIKit/UIKit.h> 
@interface ViewController : UIViewController 

    @property IBOutlet UISegmentedControl * doneButton; 

@end 


// ViewController.m 

#import "ViewController.h" 
@interface ViewController() { 

} 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self setupToolbar]; 
} 


-(void)setupToolbar { 
    self.doneButton.momentary = YES; 
    [self.doneButton addTarget:self action:@selector(donePressed:) forControlEvents:UIControlEventValueChanged]; 
} 

-(void)donePressed:(UISegmentedControl *)segmentControl { 
    NSLog(@"segmentControl index - %ld",segmentControl.selectedSegmentIndex); 
} 
@end 


스토리 보드의 값 변경 이벤트에라고 -보기 컨트롤러 인터페이스 & 함께 IBOutlet 연결 :

여기 enter image description here


대체 솔루션입니다 - 선택을 처리하는 방법 프로그래밍 방식으로 도구 모음으로. 희망이 당신을 위해 작동 할 수 있습니다.

-(void)setupToolbar{ 
    UIToolbar *toolbar = [[UIToolbar alloc] init]; 
    toolbar.frame = CGRectMake(0, 100, self.view.frame.size.width, 44); 


    UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)]; 
    item1.tag = 1; 

    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donePressed:)]; 
    item2.tag = 2; 

    NSMutableArray *items = [[NSMutableArray alloc] init]; 
    [items addObjectsFromArray:@[item1, flexibleItem, item2]]; 
    [toolbar setItems:items animated:NO]; 
    [self.view addSubview:toolbar]; 

} 

-(void)donePressed:(UIBarButtonItem *)barButtonItem { 
    NSLog(@"barButtonItem tag - %ld",barButtonItem.tag); 
} 
+0

Xcode 9 및 ios 11 장치에서 프로그래밍 방식으로 동일한 케이스를 사용해보십시오. –

+0

감사 정보 kurnal 및 질문을 업데이트했습니다. –

+0

패딩에 10과 같은 상수를 추가합니다. –