如何在UISegmentedControl更改为新段之前实现回调?

时间:2015-07-11 01:14:49

标签: ios uikit uisegmentedcontrol

使用UISegmentedControl我想在选择索引之前提出警告。问题是在选择新段后调用UIControlEventValueChanged的操作。如何在UISegmentedControl更改细分并确定是否应该更改之前实现回调?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我已经实现了UISegmentedControl的子类,以便您处理willSelectIndexshouldSelectIndex的事件并使用委托模式:

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


}