2017-10-10 72 views
0

我有一個UIToolbar並添加了幾個UISegmentedControl它。我的問題是我selectorUISegmentedControl是沒有得到所謂的IOS 11.但是在IOS 10和較低版本的正常工作。UISegmentedControl的選擇器沒有爲ios 11中的事件UIControlEventValueChanged觸發

我有一個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]; 

donePressed方法不會被調用在IOS 11.0,但相同的代碼工作在較低的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來填充。 –