- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
这个问题快把我逼疯了。当用户更改分段控件的选定“选项卡”时,我试图更改 viewController
。我花了几个小时进行研究,但未能找到有效的答案或通过 Storyboard完成的答案。
这真的很困扰我,因为设置选项卡应用程序是如此简单,但是尝试像选项卡应用程序一样使用分段控件就是行不通。我已经知道如何检测在分段控件中选择了哪个索引。我怎样才能做到这一点?
非常感谢。
最佳答案
注意:答案更新为 iOS 5+ 的 View Controller 包含代码,包括 @interface 部分
在我的一个应用程序中,我在导航栏中有一个带有段控件的 View Controller ,单击“选项卡”切换 View Controller 。基本思想是拥有一个 View Controller 数组,并使用段索引(和 indexDidChangeForSegmentedControl IBAction)在它们之间切换。
我的应用程序中的示例代码(iOS 5 或更高版本)(这是针对 2 个 View Controller 的,但它很容易扩展到多个 View Controller );代码比 iOS 4 稍长,但会保持对象图的完整性。此外,它使用 ARC:
@interface MyViewController ()
// Segmented control to switch view controllers
@property (weak, nonatomic) IBOutlet UISegmentedControl *switchViewControllers;
// Array of view controllers to switch between
@property (nonatomic, copy) NSArray *allViewControllers;
// Currently selected view controller
@property (nonatomic, strong) UIViewController *currentViewController;
@end
@implementation UpdateScoreViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// Create the score view controller
ViewControllerA *vcA = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerA"];
// Create the penalty view controller
ViewControllerB *vcB = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"];
// Add A and B view controllers to the array
self.allViewControllers = [[NSArray alloc] initWithObjects:vcA, vcB, nil];
// Ensure a view controller is loaded
self.switchViewControllers.selectedSegmentIndex = 0;
[self cycleFromViewController:self.currentViewController toViewController:[self.allViewControllers objectAtIndex:self.switchViewControllers.selectedSegmentIndex]];
}
#pragma mark - View controller switching and saving
- (void)cycleFromViewController:(UIViewController*)oldVC toViewController:(UIViewController*)newVC {
// Do nothing if we are attempting to swap to the same view controller
if (newVC == oldVC) return;
// Check the newVC is non-nil otherwise expect a crash: NSInvalidArgumentException
if (newVC) {
// Set the new view controller frame (in this case to be the size of the available screen bounds)
// Calulate any other frame animations here (e.g. for the oldVC)
newVC.view.frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
// Check the oldVC is non-nil otherwise expect a crash: NSInvalidArgumentException
if (oldVC) {
// Start both the view controller transitions
[oldVC willMoveToParentViewController:nil];
[self addChildViewController:newVC];
// Swap the view controllers
// No frame animations in this code but these would go in the animations block
[self transitionFromViewController:oldVC
toViewController:newVC
duration:0.25
options:UIViewAnimationOptionLayoutSubviews
animations:^{}
completion:^(BOOL finished) {
// Finish both the view controller transitions
[oldVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
// Store a reference to the current controller
self.currentViewController = newVC;
}];
} else {
// Otherwise we are adding a view controller for the first time
// Start the view controller transition
[self addChildViewController:newVC];
// Add the new view controller view to the ciew hierarchy
[self.view addSubview:newVC.view];
// End the view controller transition
[newVC didMoveToParentViewController:self];
// Store a reference to the current controller
self.currentViewController = newVC;
}
}
}
- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender {
NSUInteger index = sender.selectedSegmentIndex;
if (UISegmentedControlNoSegment != index) {
UIViewController *incomingViewController = [self.allViewControllers objectAtIndex:index];
[self cycleFromViewController:self.currentViewController toViewController:incomingViewController];
}
}
@end
原始示例(iOS 4 或更早版本):
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// Create the score view controller
AddHandScoreViewController *score = [self.storyboard instantiateViewControllerWithIdentifier:@"AddHandScore"];
// Create the penalty view controller
AddHandPenaltyViewController *penalty = [self.storyboard instantiateViewControllerWithIdentifier:@"AddHandPenalty"];
// Add Score and Penalty view controllers to the array
self.allViewControllers = [[NSArray alloc] initWithObjects:score, penalty, nil];
// Ensure the Score controller is loaded
self.switchViewControllers.selectedSegmentIndex = 0;
[self switchToController:[self.allViewControllers objectAtIndex:self.switchViewControllers.selectedSegmentIndex]];
}
#pragma mark - View controller switching and saving
- (void)switchToController:(UIViewController *)newVC
{
if (newVC) {
// Do nothing if we are in the same controller
if (newVC == self.currentViewController) return;
// Remove the current controller if we are loaded and shown
if([self.currentViewController isViewLoaded]) [self.currentViewController.view removeFromSuperview];
// Resize the new view controller
newVC.view.frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
// Add the new controller
[self.view addSubview:newVC.view];
// Store a reference to the current controller
self.currentViewController = newVC;
}
}
- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender {
NSUInteger index = sender.selectedSegmentIndex;
if (UISegmentedControlNoSegment != index) {
UIViewController *incomingViewController = [self.allViewControllers objectAtIndex:index];
[self switchToController:incomingViewController];
}
}
关于ios - 分段控制更改时更改 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11422845/
最近,我读了一本名为Understanding the linux kernel 的书。有一句话让我很困惑。谁能给我解释一下? As stated earlier, the Current Privi
Julia中是否有任何类型的分段/非连续范围的对象类?例如,我可以创建一个常规范围: a = UnitRange(1:5) 但是,如果我想将此与其他范围相结合: b = UnitRange([1:5,
我一直在研究标准输入的修复后计算器,在研究它之后,我最终得到了一个说法“段错误(核心转储)”,它没有说明它发生的位置或导致它的原因,并在寻找解释后我真的找不到任何可以帮助我解决这个问题的东西。所以我决
我对创建应用程序还很陌生,我刚开始使用 Swift 进行 iOS 开发。我真的希望有人能花点时间帮助我,因为我现在很困。所以我正在创建一个带有主从 TableView 的应用程序。我将使用 Alamo
ScrollView 或类似的东西中的 UIView、SegmentedController 和 UIContainerView? 在我的 Storyboard中,我有一个 VC,它在顶部包含一个 U
我需要构建一个具有任意数量的间隔和函数的分段函数,能够对输入的 numpy 数组进行操作。 我可以使用 for 循环和指示符数组来做到这一点,如下面的代码片段所示,但是有没有一种更 Pythonic
我正在尝试按照提交的方式输出我的输出,但它不起作用。比如说,如果我提交一个分为两段的帖子(通过按回车键将它们分开)它不会那样输出。它删除该段落并将帖子显示为一个段落。 显示结果: 我正在尝试按照提交的
我有一个 TableView Controller ,其中按字母顺序列出了美国所有州。单击某个状态会通过 Web 服务调用返回有关该状态的详细信息。这是我第一次尝试分段或分组 TableView ,我
我想创建一个链接节点列表。这个想法是用字母分隔一个单词,并通过每个不同的字母创建一个链接节点列表。我也尝试在最后一个位置添加最后一个节点。 我已经尝试了很多东西,理论上是可行的,但我找不到段错误的根源
这是我正在尝试创建的附加组件: import bpy import os import sys import subprocess import time from threading import
我觉得这应该是一个简单的属性,但我一直在搜索,但找不到答案。 我的表右侧有一个 TableView 索引。选择后,它会使所有内容变灰。我想模仿 iPod 应用程序,当索引本身被选中时,它是半透明的(可
我试图在 OpenCV 中使用 SLIC 分割图像。我正在尝试使用以下功能: void vl_slic_segment ( vl_uint32 * segmentation, flo
我知道在网络中,数据流在第4层被分成不同的段。每个段然后用端口号和IP地址封装。我发现了一些我想问的令人困惑的问题: 谁负责将数据流分成不同的段。是应用程序还是第 4 层? 我知道 UDP 不支持分段
我有一个使用界面生成器制作的分段 Controller ,它看起来像这样 有时我使用它将它设置为四个 [segmentedControl insertSegmentWithTitle:@"Dinner
所以我使用免费托管,我的服务器上几乎没有视频,一切正常,除了视频需要大约 10-20 秒来加载然后开始播放,所以我想使用流媒体。问题是我不知道该字段和托管不支持 node.js,但我认为是 php 7
ViewController 有三个按钮:“ friend ”“日历”“信息”。每个按钮将代表此 viewController 下半部分的不同信息。 我的问题是选择什么来表示这些信息 - 分段 Con
在我的应用程序中,弹出窗口中的 UITableView 会在新数据时重新加载。如果表格未分区,则使用此代码一切都可以完美运行: 通知,启动重新加载: - (void)recieveNotificati
在阅读muduo(C++网络库)的源码时提出了这个问题。 如果客户端发送一个大消息,它将被 TCP 分段,服务器端会发生什么? (服务器是否知道这条消息已经被分段了?) 网络库是否需要等待整个消息并且
我正在重构一个包含大量长头文件的代码库(为了便于使用和简化编译,头文件同时包含接口(interface)和实现)。 代码库避免了像野火一样的多态性,因此它使用宏解析内部存储类型,如下所示: #if d
我正在阅读有关 x86 保护模式工作的信息,因为我已经看到了平面内存模型和分段内存模型。 如果 linux 内核使用平面内存模型,那么它如何保护非特权应用程序对关键数据的访问? 最佳答案 Linux
我是一名优秀的程序员,十分优秀!