使用UISegmentedControl
我想在選擇索引之前出現警告。問題是UIControlEventValueChanged
的操作在選擇新的段後調用。我如何在UISegmentedControl
更改細分並決定是否應該更改之前實施回調?如何在UISegmentedControl更改爲新段之前實現回調?
1
A
回答
1
您可以按「招」通過抓住selectedSegmentIndex值,然後立即將控制selectedSegmentIndex值設置爲-1控制的,所以選擇基本上被推遲。
有兩段一個基本的例子會是這樣的 -
- (IBAction)segmentedIndexDidChange:(UISegmentedControl*)sender {
// grab the segment index value and store
NSInteger indexToQuery = sender.selectedSegmentIndex;
// deselect any segments while making decisions
sender.selectedSegmentIndex = -1;
// now we have a willChange scenario instead of didChange
switch (indexToQuery) {
case 0: {
NSLog(@"seg index will change to 0");
//.. do stuff .. make decisions .. etc.
break;
}
case 1: {
NSLog(@"seg index will change to 1");
//.. do stuff
break;
}
default:
break;
}
}
// remember to config the segmented control after all is done
0
我已實施的UISegmentedControl
一個子類爲您辦理的willSelectIndex
和shouldSelectIndex
事件和使用委託模式:
ITDSegmentedControl.h
#import <UIKit/UIKit.h>
@class ITDSegmentedControl;
@protocol ITDSegmentedControlDelegate <NSObject>
@optional
-(void) segmentedControl:(ITDSegmentedControl *)segmentedControl willSelectIndex:(NSNumber *) index;
-(void) segmentedControl:(ITDSegmentedControl *)segmentedControl shouldSelectIndex:(NSNumber *) index andHandler:(void(^)(BOOL result))handler;
@end
@interface ITDSegmentedControl : UISegmentedControl
@property (nonatomic, weak) id<ITDSegmentedControlDelegate> delegate;
@end
ITDSegmentedControl.h
#import "ITDSegmentedControl.h"
@implementation ITDSegmentedControl
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGFloat x = [touch locationInView:self].x;
// Ignore touches that don't matter
if (x < 0 || x > self.frame.size.width) {
return;
}
NSUInteger index = (NSUInteger)floorf((CGFloat)x/(self.frame.size.width/(CGFloat)[self numberOfSegments]));
if ([self isEnabledForSegmentAtIndex:index]) {
if ([self.delegate respondsToSelector:@selector(segmentedControl:shouldSelectIndex:andHandler:)]) {
[self.delegate segmentedControl:self shouldSelectIndex:[NSNumber numberWithInteger:index] andHandler:^(BOOL result) {
if (result) {
if ([self.delegate respondsToSelector:@selector(segmentedControl:willSelectIndex:)]) {
[self.delegate performSelector:@selector(segmentedControl:willSelectIndex:) withObject:self withObject:[NSNumber numberWithInteger:index]];
self.selectedSegmentIndex = (NSInteger)index;
}
}
}];
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (!self.momentary) {
return;
}
self.selectedSegmentIndex = UISegmentedControlNoSegment;
[self setNeedsDisplay];
}
- (void)setSelectedSegmentIndex:(NSInteger)index {
[super setSelectedSegmentIndex:index];
}
@end
ViewController.h
#import "ITDSegmentedControl.h"
@interface ViewController: UIViewController <ITDSegmentedControlDelegate>
@property (weak, nonatomic) IBOutlet ITDSegmentedControl *mySegmented;
@end
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.mySegmented.delegate = self;
}
-(void) segmentedControl:(ITDSegmentedControl *)segmentedControl willSelectIndex:(NSNumber *) index
{
NSLog(@"willSelectIndex %zd",[index integerValue]);
}
-(void) segmentedControl:(ITDSegmentedControl *)segmentedControl shouldSelectIndex:(NSNumber *) index andHandler:(void(^)(BOOL result))handler
{
NSLog(@"shouldSelectIndex %zd",[index integerValue]);
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Alert"
message:@"Are you Sure"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
handler(NO);
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
handler(YES);
NSLog(@"OK action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
相關問題
- 1. UISegmentedControl選擇不動畫之前更新
- 2. 如何更改UISegmentedControl中禁用段的色調顏色
- 3. 字段值不被修改的更新前的回調
- 4. 如何在laravel更新數據庫之前更改字段的值
- 5. 如何在更新觸發之前更改插入觸發器之前?
- 6. 如何在更新之前使用返回來返回列?
- 7. MySQL:如何在更改之前使用值更新列
- 8. 在更新之前驗證字段
- 9. UISegmentedControl禁用段而不更改字體
- 10. PHP類字段在得到返回之前得到更改
- 11. 在模型的「新」之前回調rails?
- 12. 在Python和C之間實現回調
- 13. 如何修改AJAX調用在jqGrid中呈現之前返回的數據?
- 14. RadContextMenu.DropDownOpened在更改RadGridView.CurrentRow之前調用
- 15. GreenDao如何實現回調
- 16. 使兩個sql字段需要彼此更新之前更改
- 17. 如何更改之前在懸停
- 18. 如何在使用phpfox呈現之前更改變量?
- 19. 如何更改JAXB實現?
- 20. 更改UISegmentedControl文本
- 21. 防止UISegmentedControl更改
- 22. Symfony2:如何在做login_check之前更改實體管理器
- 23. 如何實現,如果之前同時
- 24. 如何改變UISegmentedControl
- 25. 在返回方法調用之前更新本地iqueryable列表
- 26. setValue:在啓用狀態更改之前沒有爲文本字段調用
- 27. 之前保存回調布爾字段
- 28. 如何在applicationDidBecomeActive之前更新屏幕?
- 29. C#[4.0之前] - 如何在長度計算回調後更新GUI
- 30. 如何更改UISegmentedControl中的值?
我已經更新了我的答案,我已經實現ŧ他正在尋找的行爲。 – Aladin