2015-07-11 26 views

回答

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一個子類爲您辦理的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]; 


} 
相關問題