【问题标题】:Switching views in UISegmentedControl in UIView在 UIView 中的 UISegmentedControl 中切换视图
【发布时间】:2016-06-16 20:42:27
【问题描述】:

我遇到了切换UISegmentedControl 视图的问题。我已经导入了HMSegmentedControl 的框架来使用多个视图(7 个视图)。我想在点击新匹配时切换视图,它应该显示匹配视图,当点击每日推荐时,它应该显示匹配视图等等。下面是我的代码。我在segmentedControlChangedValue 切换视图方面尝试了很多,但它对我不起作用。有什么事情要做还是我做错了什么?

  @interface MatchesViewController ()
  @property (nonatomic, strong) UIScrollView *scrollView;
  @property (strong, nonatomic) IBOutlet UIScrollView *scrollVw;
  @property (strong, nonatomic) IBOutlet UIView *matchesView;
  @property (strong, nonatomic) IBOutlet UIView *dailyRecommendationsView;
  @property (strong, nonatomic) IBOutlet UIView *preferedMatchesView;
  @property (strong, nonatomic) IBOutlet UIView *broaderMatchesView;
  @property (strong, nonatomic) IBOutlet UIView *twowayMatcheVsiew;
  @property (strong, nonatomic) IBOutlet UIView *reverseMatchesView;
  @property (strong, nonatomic) IBOutlet UIView *decideLaterView;
  - (IBAction)btnMenuClicked:(id)sender;
  @end

  @implementation MatchesViewController

  - (void)viewDidLoad {
  [super viewDidLoad];
  self.title = @"Matches";
  self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:16/255.0 green:97/255.0 blue:61/255.0 alpha:1.0];

  [self.navigationController.navigationBar
  setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];


  [self.scrollVw addSubview:self.matchesView];
  [self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.matchesView.frame.size.height)];

  [self.scrollVw addSubview:self.dailyRecommendationsView];
  [self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.dailyRecommendationsView.frame.size.height)];

  [self.scrollVw addSubview:self.preferedMatchesView];
  [self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.preferedMatchesView.frame.size.height)];

  [self.scrollVw addSubview:self.broaderMatchesView];
  [self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.broaderMatchesView.frame.size.height)];

  [self.scrollVw addSubview:self.twowayMatcheVsiew];
  [self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.twowayMatcheVsiew.frame.size.height)];

  [self.scrollVw addSubview:self.reverseMatchesView];
  [self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.reverseMatchesView.frame.size.height)];

  [self.scrollVw addSubview:self.decideLaterView];
  [self.scrollVw setContentSize:CGSizeMake(self.view.frame.size.width, self.decideLaterView.frame.size.height)];

  self.view.backgroundColor = [UIColor clearColor];
  self.edgesForExtendedLayout = UIRectEdgeNone;


  CGFloat viewWidth = CGRectGetWidth(self.view.frame);
  // Do any additional setup after loading the view from its nib.
  HMSegmentedControl *segmentedControl1 = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"New Matches", @"Daily Recommendations", @"Prefered Matches", @"Broader Matches", @"2-Way Matches", @"Reverse Matches", @"Decide Later"]];
  segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
  segmentedControl1.frame = CGRectMake(0, 0, viewWidth, 45);
  segmentedControl1.segmentEdgeInset = UIEdgeInsetsMake(0, 5, 0, 5);
  segmentedControl1.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
  segmentedControl1.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;


  segmentedControl1.backgroundColor = [UIColor colorWithRed:167/255.0 green:148/255.0 blue:4/255.0 alpha:1.0];

  //segmentedControl1.selectionStyle = HMSegmentedControlSelectionStyleBox;
  //segmentedControl1.selectedSegmentIndex = HMSegmentedControlNoSegment;


  segmentedControl1.selectionIndicatorColor = [UIColor whiteColor];
  segmentedControl1.selectionIndicatorHeight = 2.0f;
  segmentedControl1.verticalDividerEnabled = YES;
  segmentedControl1.verticalDividerColor = [UIColor whiteColor];
  segmentedControl1.verticalDividerWidth = 1.0f;
  [segmentedControl1 setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
  NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
  return attString;
  }];

  [segmentedControl1 addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
  [self.view addSubview:segmentedControl1];

  [self.navigationController.navigationBar
  setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

  }


  - (void)segmentedControlChangedValue:(UISegmentedControl *)segment
  {

  switch (segment.selectedSegmentIndex) {
  case 0:
  self.matchesView.hidden = NO;
  self.dailyRecommendationsView.hidden = YES;
  self.preferedMatchesView.hidden = YES;
  self.broaderMatchesView.hidden = YES;
  self.twowayMatcheVsiew.hidden = YES;
  self.reverseMatchesView.hidden = YES;
  self.decideLaterView.hidden = YES;
  break;
  case 1:
  self.matchesView.hidden = YES;
  self.dailyRecommendationsView.hidden = NO;
  self.preferedMatchesView.hidden = YES;
  self.broaderMatchesView.hidden = YES;
  self.twowayMatcheVsiew.hidden = YES;
  self.reverseMatchesView.hidden = YES;
  self.decideLaterView.hidden = YES;
  break;
  case 2:
  self.matchesView.hidden = YES;
  self.dailyRecommendationsView.hidden = YES;
  self.preferedMatchesView.hidden = NO;
  self.broaderMatchesView.hidden = YES;
  self.twowayMatcheVsiew.hidden = YES;
  self.reverseMatchesView.hidden = YES;
  self.decideLaterView.hidden = YES;
  break;
  case 3:
  self.matchesView.hidden = YES;
  self.dailyRecommendationsView.hidden = YES;
  self.preferedMatchesView.hidden = YES;
  self.broaderMatchesView.hidden = NO;
  self.twowayMatcheVsiew.hidden = YES;
  self.reverseMatchesView.hidden = YES;
  self.decideLaterView.hidden = YES;
  break;
  case 4:
  self.matchesView.hidden = YES;
  self.dailyRecommendationsView.hidden = YES;
  self.preferedMatchesView.hidden = YES;
  self.broaderMatchesView.hidden = YES;
  self.twowayMatcheVsiew.hidden = NO;
  self.reverseMatchesView.hidden = YES;
  self.decideLaterView.hidden = YES;
  break;
  case 5:
  self.matchesView.hidden = YES;
  self.dailyRecommendationsView.hidden = YES;
  self.preferedMatchesView.hidden = YES;
  self.broaderMatchesView.hidden = YES;
  self.twowayMatcheVsiew.hidden = YES;
  self.reverseMatchesView.hidden = NO;
  self.decideLaterView.hidden = YES;
  break;
  case 6:
  self.matchesView.hidden = YES;
  self.dailyRecommendationsView.hidden = YES;
  self.preferedMatchesView.hidden = YES;
  self.broaderMatchesView.hidden = YES;
  self.twowayMatcheVsiew.hidden = YES;
  self.reverseMatchesView.hidden = YES;
  self.decideLaterView.hidden = NO;
  break;
  default:
  break;
  }

  }

  - (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl {
  NSLog(@"Selected index %ld", (long)segmentedControl.selectedSegmentIndex);

  }

【问题讨论】:

    标签: ios objective-c iphone xcode uisegmentedcontrol


    【解决方案1】:

    你的方法segmentedControlChangedValue 被调用了吗?

    尝试在其上添加断点并跟踪 segment.selectedSegmentIndex 的值。我认为问题出在那儿。

    由于 HMSegmentedControl 不是 UISegmentedControl 的子类,而是 UIControl 的子类,所以方法应该是:

      - (void)segmentedControlChangedValue:(HMSegmentedControl *)segment
    

    而不是:

      - (void)segmentedControlChangedValue:(UISegmentedControl *)segment
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:
      [segmentedControl1 addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
      

      事件不正确。 尝试执行以下代码

      [segmentedControl1 addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventTouchUpInside];
      

      重新安装您的应用。

      【讨论】:

        猜你喜欢
        • 2011-07-13
        • 2012-05-15
        • 2015-09-23
        • 2010-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-25
        • 2011-01-08
        相关资源
        最近更新 更多