[英]How do I use a UISegmentedControl to switch views?
我正試圖弄清楚如何使用UISegmentedControl的不同狀態來切換視圖,類似於Apple在“頂級付費”和“頂級免費”之間切換時在App Store中的操作方式。
最簡單的方法是使用兩個視圖來切換其可見性以指示已選擇哪個視圖。 下面是一些關於如何完成它的示例代碼,絕對不是處理視圖的優化方法,而只是為了演示如何使用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];
}
}
您當然可以進一步重新調整代碼以隱藏/顯示正確的視圖。
在我的情況下,我的觀點非常復雜,我不能只改變不同視圖的隱藏屬性,因為它會占用太多內存。
我已經嘗試了幾種解決方案,並且它們不適用於我,或者執行不正常,特別是在推送/彈出視圖時,navBar的titleView並不總是顯示segmentedControl。
我發現這篇關於該問題的博客文章解釋了如何以正確的方式做到這一點。 似乎他在2010年WWDC上得到了蘋果工程師的幫助,想出了這個解決方案。
http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited
此鏈接中的解決方案是我迄今為止發現的有關此問題的最佳解決方案。 通過一點調整,它也可以在底部的tabBar中正常工作
或者,如果是表,則可以重新加載表,並在cellForRowAtIndex中,根據所選的段選項從不同的數據源填充表。
一個想法是讓具有分段控件的視圖具有容器視圖,您可以使用不同的子視圖填充(在切換段時添加為容器視圖的唯一子視圖)。 你甚至可以為這些子視圖設置單獨的視圖控制器,但如果你需要它們,你必須轉發重要的方法,如“viewWillAppear”和“viewWillDisappear”(並且必須告訴他們它們所屬的導航控制器)。
通常情況下效果很好,因為您可以在IB中布置帶容器的主視圖,子視圖將填充容器允許的任何空間(確保正確設置自動調整大小的掩碼)。
試試這段代碼,這將幫助您在更改Segment COntrol片段的不同視圖之間切換
嘗試使用SNFSegmentedViewController
,這是一個開源組件,可以使用像UITabBarController
這樣的設置來完成您正在尋找的內容。
分配.H in
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];
}
}
從@Ronnie Liew的回答中,我創建了這個:
//
// 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
Swift版本:
父視圖控制器負責設置每個子視圖控制器的視圖的大小和位置。 子視圖控制器的視圖成為父視圖控制器的視圖層次結構的一部分。
定義惰性屬性:
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-view-controllers/
一個快速的Swift版本:
@IBAction func segmentControlValueChanged(_ sender: UISegmentedControl) {
if segmentControl.selectedSegmentIndex == 0 {
// do something
} else {
// do something else
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.