どのように私は、ビューを切り替えるにはUISegmentedControlを使用していますか?

StackOverflow https://stackoverflow.com/questions/1047114

質問

私は「有料トップ」と「トップ無料」の間switichingとき、Appleがアプリケーションストアでそれをしない方法に似ビューを切り替えるにはUISegmentedControlの異なる状態を使用する方法を把握しようとしています。

役に立ちましたか?

解決

最も簡単な方法は、あなたが選択されたビューを示すために、彼らの表示を切り替えることができる2つのビューを持つことです。ここではそれを行うことができる方法についていくつかのサンプルコード、間違いではないのビューを処理するために、ちょうどあなたが目に見えるビューを切り替えるには、のUISegmentControl を使用する方法を示すために最適化された方法は以下のとおりです。

- (IBAction)segmentSwitch:(id)sender {
  UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
  NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;

  if (selectedSegment == 0) {
    //toggle the correct view to be visible
    [firstView setHidden:NO];
    [secondView setHidden:YES];
  }
  else{
    //toggle the correct view to be visible
    [firstView setHidden:YES];
    [secondView setHidden:NO];
  }
}


あなたはもちろん、さらに右のビューを表示/非表示するコードをファクタし直すことができます。

他のヒント

私の場合は私の意見は非常に複雑であり、それはあまりにも多くのメモリを取るだろうので、私はちょうど異なるビューの隠されたプロパティを変更することはできません。

私はいくつかのソリューションおよびそれらの非は私のために働いてみました、または不規則に行われ、特別のビューをポップ/押すとナビゲーションバーのtitleViewは常にsegmentedControlを示していないとしました。

私は、適切な方法でそれを行う方法を説明し、問題については、このブログの記事を見つけました。彼はこの解決策を考え出すWWDC'2010でアップルのエンジニアの助けを持っていたようです。

http://redartisan.com/2010/6/ 27 / uisegmented制御視点切替再訪する

このリンクでの解決策は、私がこれまで問題について発見した最適なソリューションダウンの手です。調整の少しで、それはまた、

下部のタブバーでうまく働い

またはそのテーブル場合は、テーブルをリロードしcellForRowAtIndexで、選択したセグメントオプションに基づいて、異なるデータソースからテーブルを読み込むことができます。

ひとつのアイデアは、セグメント化されたコントロールを使用すると、別のサブビュー(セグメントが切り替えられたときにコンテナビューの唯一のサブビューとして追加)で満たすコンテナビューを持っているとの見解を持つことです。あなたがそれらを必要とする場合は、「viewWillAppear」と「viewWillDisappear」のような重要な方法で転送するように(と彼らが下にあるものを、ナビゲーションコントローラ告げなければならないであろう)があるもののあなたも、それらのサブビューに別々のView Controllerを持つことができます。

一般的に、あなたがIBで容器にメインビューをレイアウトすることができ、及びサブビューは、コンテナがそれらを(あなたの自動サイズ変更マスクが正しく設定されていることを確認してください)を持つことができますどのようなスペースいっぱいになりますので、かなりうまく機能します。

この意志はあなたがセグメント制御の変更セグメント

上の異なるビューを切り替えるのに役立ちます、このコードを試してみてください UISegmentControlの異なるセグメントを選択する上で

開き異なるビュー

あなたが探しているまさにん SNFSegmentedViewController に、オープンソースコンポーネントを使用してみてください以下のようなセットアップでUITabBarController

@Ronnieリューの答えから、私はこれを作成します:

//
//  ViewController.m
//  ResearchSegmentedView
//
//  Created by Ta Quoc Viet on 5/1/14.
//  Copyright (c) 2014 Ta Quoc Viet. All rights reserved.
//
#define SIZE_OF_SEGMENT 56
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize theSegmentControl;
UIView *firstView;
UIView *secondView;
CGRect leftRect;
CGRect centerRect;
CGRect rightRect;
- (void)viewDidLoad
{
    [super viewDidLoad];
    leftRect = CGRectMake(-self.view.frame.size.width, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT);
    centerRect = CGRectMake(0, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT);
    rightRect = CGRectMake(self.view.frame.size.width, SIZE_OF_SEGMENT, self.view.frame.size.width, self.view.frame.size.height-SIZE_OF_SEGMENT);

    firstView = [[UIView alloc] initWithFrame:centerRect];
    [firstView setBackgroundColor:[UIColor orangeColor]];
    secondView = [[UIView alloc] initWithFrame:rightRect];
    [secondView setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:firstView];
    [self.view addSubview:secondView];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)segmentSwitch:(UISegmentedControl*)sender {
    NSInteger selectedSegment = sender.selectedSegmentIndex;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2];
    if (selectedSegment == 0) {
        //toggle the correct view to be visible
        firstView.frame = centerRect;
        secondView.frame = rightRect;
    }
    else{
        //toggle the correct view to be visible
        firstView.frame = leftRect;
        secondView.frame = centerRect;
    }
    [UIView commitAnimations];
}
@end

を割り当てます.H

 UISegmentedControl *lblSegChange;

- (IBAction)segValChange:(UISegmentedControl *) sender

.Mを宣言します。

- (IBAction)segValChange:(UISegmentedControl *) sender
{

 if(sender.selectedSegmentIndex==0)
 {
  viewcontroller1 *View=[[viewcontroller alloc]init];
  [self.navigationController pushViewController:view animated:YES];
 }
 else 
 {
  viewcontroller2 *View2=[[viewcontroller2 alloc]init];
  [self.navigationController pushViewController:view2 animated:YES];
 }
} 

スウィフトバージョン:

親ビューコントローラは、各子ビューコントローラのビューのサイズと位置を設定するための責任があります。子ビューコントローラのビューは、親ビューコントローラのビュー階層の一部になります。

怠惰なプロパティを定義します:

private lazy var summaryViewController: SummaryViewController = {
   // Load Storyboard
   let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

   // Instantiate View Controller
   var viewController = storyboard.instantiateViewController(withIdentifier: "SummaryViewController") as! SummaryViewController

   // Add View Controller as Child View Controller
   self.add(asChildViewController: viewController)

   return viewController
}()

private lazy var sessionsViewController: SessionsViewController = {
    // Load Storyboard
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)

    // Instantiate View Controller
    var viewController = storyboard.instantiateViewController(withIdentifier: "SessionsViewController") as! SessionsViewController

    // Add View Controller as Child View Controller
    self.add(asChildViewController: viewController)

    return viewController
}()

の表示/非表示子ビューコントローラーます:

private func add(asChildViewController viewController: UIViewController) {
    // Add Child View Controller
    addChildViewController(viewController)

    // Add Child View as Subview
    view.addSubview(viewController.view)

    // Configure Child View
    viewController.view.frame = view.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    // Notify Child View Controller
    viewController.didMove(toParentViewController: self)
}

private func remove(asChildViewController viewController: UIViewController) {
    // Notify Child View Controller
    viewController.willMove(toParentViewController: nil)

    // Remove Child View From Superview
    viewController.view.removeFromSuperview()

    // Notify Child View Controller
    viewController.removeFromParentViewController()
}

SegmentedControl tapEventを管理

private func updateView() {
    if segmentedControl.selectedSegmentIndex == 0 {
        remove(asChildViewController: sessionsViewController)
        add(asChildViewController: summaryViewController)
    } else {
        remove(asChildViewController: summaryViewController)
        add(asChildViewController: sessionsViewController)
    }
}

そしてもちろん、あなたの子ビューコントローラクラス内で使用することができます:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("Summary View Controller Will Appear")
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    print("Summary View Controller Will Disappear")
}

リファレンス: https://cocoacasts.com/managing-view-controllers-with -container - ビュー - コントローラ/

を迅速スウィフトバージョン:

@IBAction func segmentControlValueChanged(_ sender: UISegmentedControl) {

    if segmentControl.selectedSegmentIndex == 0 {

        // do something
    } else {

        // do something else
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top