gpt4 book ai didi

iphone - Objective c - 使用一个或多个 ViewController 在 View 之间翻转?

转载 作者:行者123 更新时间:2023-12-01 17:19:46 25 4
gpt4 key购买 nike

我正在构建一个位置应用程序,向用户展示他周围的一些地方。
我有一个 NearbyPlacesViewController带有一个具有两个按钮“列表”和“ map ”的段控件

如果用户按下“列表” - 我向他展示一个表格 View ,其中包含他周围的地方列表
如果用户按下“ map ” - View 会翻转,我会向用户显示一个 map View ,其中的位置作为图钉。

在“列表”中,我还使用 UISearchBar 和 UISearchDisplayController 来搜索 tableView
在“ map ”中,我在 mapView 旁边还有一些其他 subview

目前我保留所有 View (UITableView, MKMapView, UISearchBar 和更多...)
和委托(delegate)方法( UITableViewDelegate, UITableViewDataSource, MKMapViewDelegate, UISearchDisplayDelegate 等等..)
NearbyPlacesViewController .

当用户按下“ map ”按钮时,我会隐藏与“列表” View (tableView、搜索栏...)相关的所有 View ,并取消隐藏与“ map ” View 相关的所有 View (mapView、其他一些 subview ...),然后我使用UIView transitionWithView在它们之间做翻转动画。

一切正常,但看起来有点乱,结果是一个很大的 NearbyPlacesViewController 有很多代码和委托(delegate)方法。

使用单独的 View Controller 会更好吗?
如果是这样,我该怎么做?如何创建 ListViewController 和 MapViewController 并将它们放在 NearbyViewController 中?
我如何在他们之间共享模型?

最佳答案

只是为了让您了解 View Controller 包含可能是什么样子(iOS 5),这是一种方法。它由四个类组成,容器 View Controller (其 View 将具有用于在两个 subview Controller 之间切换的分段控制)、两个随机 subview Controller 和一个我们将存储数据的模型类(可以通过两个 subview Controller )。

首先,您使用分段控件创建一个容器 View Controller (我还添加了一个 UIView,它基本上定义了放置 subview Controller View 的框架,只是为了更容易弄清楚该 View 的放置位置):

//  ContainerViewController.h

#import <UIKit/UIKit.h>

@interface ContainerViewController : UIViewController

@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
@property (weak, nonatomic) IBOutlet UIView *childView;

- (IBAction)changeChild:(id)sender;

@end

然后你实现它:
//  ContainerViewController.m

#import "ContainerViewController.h"
#import "FirstContainedViewController.h"
#import "SecondContainedViewController.h"
#import "MyModel.h"

@interface ContainerViewController ()
{
FirstContainedViewController *_controller0;
SecondContainedViewController *_controller1;
MyModel *_model;

UIViewController __weak *_currentChildController; // let's keep track of the current
}

@end

@implementation ContainerViewController

@synthesize segmentedControl = _segmentedControl;
@synthesize childView = _childView;

- (void)dealloc
{
// let's release our child controllers

_controller0 = nil;
_controller1 = nil;

// and release the model, too

_model = nil;
}

// this is my own method to
// 1. add the child view controller to the view controller hierarchy;
// 2. do the appropriate notification (even though I don't use it, Apple says you should do this, so I will); and
// 3. set the frame size to the appropriate size

- (void)addChildToThisContainerViewController:(UIViewController *)childController
{
[self addChildViewController:childController];
[childController didMoveToParentViewController:self];
childController.view.frame = CGRectMake(0.0,
0.0,
self.childView.frame.size.width,
self.childView.frame.size.height);
}

- (void)viewDidLoad
{
[super viewDidLoad];

// let's create our model, our data

_model = [[MyModel alloc] init];

// set the segmented index to point to the first one

[self.segmentedControl setSelectedSegmentIndex:0];

// let's create our two controllers and provide each a pointer to our model

_controller0 = [[FirstContainedViewController alloc] initWithNibName:@"FirstContainedView" bundle:nil];
_controller0.model = _model;

_controller1 = [[SecondContainedViewController alloc] initWithNibName:@"SecondContainedView" bundle:nil];
_controller1.model = _model;

// let's add them to the view controller hierarchy

[self addChildToThisContainerViewController:_controller0];
[self addChildToThisContainerViewController:_controller1];

// let's add the currently selected controller as the "current child controller" and add it to our current view

_currentChildController = [self.childViewControllers objectAtIndex:self.segmentedControl.selectedSegmentIndex];
[self.childView addSubview:_currentChildController.view];
}

- (void)viewDidUnload
{
[self setChildView:nil];
[self setSegmentedControl:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (IBAction)segmentedControlValueChanged:(UISegmentedControl *)sender
{
UIViewController *oldChildController = _currentChildController;
UIViewController *newChildController = [self.childViewControllers objectAtIndex:sender.selectedSegmentIndex];
UIViewAnimationOptions options;

// let's change the animation based upon which segmented control you select ... you may change this as fits your desired UI

if (sender.selectedSegmentIndex == 0)
options = UIViewAnimationOptionTransitionFlipFromLeft;
else
options = UIViewAnimationOptionTransitionFlipFromRight;

[self transitionFromViewController:oldChildController
toViewController:newChildController
duration:0.5
options:options
animations:nil
completion:nil];

_currentChildController = newChildController;
}

@end

我的模型只有两个数据元素、一个对象数组和一个字符串,但显然你可以为所欲为。我将只显示标题(因为实现细节微不足道且无趣):
//  MyModel.h

#import <Foundation/Foundation.h>

@interface MyModel : NSObject

@property (nonatomic, strong) NSMutableArray *listOfItems;
@property (nonatomic, strong) NSString *displayText;

- (MyModel *)init;

@end

subview Controller 同样简单:
//  FirstContainedViewController.h

#import <UIKit/UIKit.h>

@class MyModel;

@interface FirstContainedViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) MyModel *model;

@end

实现可能看起来像(这是一个简单的示例,但显示了如何从共享模型中检索信息):
//  FirstContainedViewController.m

#import "FirstContainedViewController.h"
#import "MyModel.h"

@implementation FirstContainedViewController

@synthesize model = _model;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - tableview data source delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.model.listOfItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"fcvc";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

cell.textLabel.text = [self.model.listOfItems objectAtIndex:indexPath.row];

return cell;
}

@end

希望这能让您了解如何为两个 View 使用单独的 View Controller ,如何在它们之间切换,使您的模型可供两者访问。这是一个相当简单的例子,但它很实用。我可能会建议一些优化,但希望这足以让你朝着正确的方向前进。

关于iphone - Objective c - 使用一个或多个 ViewController 在 View 之间翻转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11171404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com