我想弄清楚如何使用UISegmentedControl的不同狀態來切換視圖,類似於蘋果公司在App Store中在'Top Paid'和'Top Free'之間切換時的操作。如何使用UISegmentedControl切換視圖?
回答
最簡單的方法是有兩個視圖,您可以切換其可見性以指示已選擇哪個視圖。下面是它如何做,絕對不是一個優化的方式來處理的意見,但只是爲了演示如何使用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];
}
}
您當然可以進一步重新編碼以隱藏/顯示正確的視圖。
或者,如果它的表,您可以重新加載表,並在cellForRowAtIndex,根據選定的段選項從不同的數據源填充表。
一個想法是讓分段控件的視圖有一個容器視圖,您可以填充不同的子視圖(在切換切片時添加爲容器視圖的唯一子視圖)。你甚至可以爲這些子視圖設置單獨的視圖控制器,但是如果你需要它們(並且必須知道它們在哪個導航控制器下),你必須轉發諸如「viewWillAppear」和「viewWillDisappear」之類的重要方法。
通常情況下,因爲您可以使用容器在IB中佈局主視圖,並且子視圖將填充容器允許其擁有的任何空間(確保您的autoresize掩碼設置正確)。
就我而言,我的觀點非常複雜,我不能只是改變不同視圖的隱藏屬性,因爲它會佔用太多的內存。
我已經嘗試了幾種解決方案,並且它們都不適合我,或者執行不正常,特別是navBar的titleView在推/彈出視圖時並不總是顯示segmentedControl。
我發現這篇博客文章解釋瞭如何以正確的方式做到這一點。似乎他得到了WWDC'2010的Apple工程師的幫助,想出了這個解決方案。
http://redartisan.com/2010/6/27/uisegmented-control-view-switching-revisited
在這個環節的解決方案是手了最好的解決方案,我迄今發現有關問題。隨着調整的一點點,也工作得很好,在底部
一個的TabBar試試這個代碼,這將幫助您在不同視圖之間的分段控制的變化段
Open Different Views on selecting different segments of UISegmentControl
嘗試使用切換SNFSegmentedViewController
,這是一個開源組件,它完全按照您要尋找的設置,如UITabBarController
。
分配.H在
UISegmentedControl *lblSegChange;
- (IBAction)segValChange:(UISegmentedControl *) sender
申報。中號
- (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劉氏的答案,我創建這個:
//
// 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
快速斯威夫特版本:
@IBAction func segmentControlValueChanged(_ sender: UISegmentedControl) {
if segmentControl.selectedSegmentIndex == 0 {
// do something
} else {
// do something else
}
}
- 1. UISegmentedcontrol在UITabbarcontroller中切換視圖
- 2. 在UIView中切換UISegmentedControl中的視圖
- 3. 與UISegmentedControl切換視圖與selectedSegmentIndex
- 4. 使用UISegmentedControl切換NSFetchedResultsController
- 5. 使用UISegmentedControl切換UIViews
- 6. 如何使用UIActionSheet切換視圖?
- 7. 在iOS5中使用UISegmentedControl切換ViewControllers
- 8. 使用UISegmentedControl切換到MKMapView和UITableView
- 9. UISegmentedControl - 如何切換2個UITableViews
- 10. 如何以編程方式切換使用視圖切換?
- 11. 使用modalViewControllers切換視圖
- 12. 使用UIAccelerometer切換視圖
- 13. 使用UINavigationController切換視圖
- 14. Knockout.js - 如何切換視圖
- 15. 如何在視圖內切換視圖?
- 16. UISegmentedControl切換中的UITableViewController
- 17. 用UISegmentedControl在UIViewControllers之間切換
- 18. 視圖切換
- 19. 切換視圖
- 20. 切換視圖
- 21. 切換視圖
- 22. 切換視圖
- 23. 使用UISegmentedControl選擇子視圖
- 24. 用PRISM切換主視圖
- 25. 使用ActivityGroup切換視圖導致StackOverflowError
- 26. 問題使用CATransitions切換視圖
- 27. iOS:使用UINavigationBarController切換視圖
- 28. 使用ViewFlipper和onclick切換視圖。
- 29. 在Xamarin.Forms中切換視圖(使用XAML)
- 30. 使用UINavigationBar在視圖之間切換
「絕對不是一種優化的方式來處理的意見' - 爲什麼? – 2012-11-22 21:52:54
@AdamWaite因爲所有視圖都必須永久存儲在內存中。如果您的觀點太複雜和/或包含許多其他元素,將會影響整體表現。這段代碼也可以重構。 – Stas 2013-07-02 05:23:45
@Stas你是對的,最好將多個視圖控制器之間的邏輯分開,每個負責他自己的行爲和行爲 – 2015-02-18 21:09:00