initWithRootViewController、init…

本文深入探讨了UINavigationController的栈式管理机制,详细解释了如何通过`initWithRootViewController`、`pushViewController`和`popViewControllerAnimated`等关键函数进行ViewController的添加与移除操作。同时介绍了`initWithNibName`函数在使用Xib文件初始化控制器时的作用,以及在iPad开发中的应用。

1、UINavigationController通过栈来实现。添加一个Controller为入栈,释放一个Controller为出栈。复习下栈:

1)栈是先进后出

2)栈顶是最后一个入栈的对象

3)基栈是是第一个入栈的对象(栈底)

UINavigationController经常使用的函数:

(1)- (id)initWithRootViewController:(UIViewController *)rootViewController

添加根视图控制器,最先显示。

(2)- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

(3)- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

Controller出栈操作

这里要注意:每次push Controller后,Controller系统返回按钮实现pop这个Controller。

2、initWithStyle是UITableViewController提供的初始化函数,可以初始化为plain或grouped类型,当前VC为UITableViewController或其子类时使用

1)UITableView继承自UIScrollView,可以表现为Plain和Grouped两种风格

2)UITableView继承自UIScrollView

3)UITableView有两个Delegate:dataSource和delegate

3)UITableViewController是系统提供用来管理UITableView的类,自身包含了TableView的UITableViewDataSourceDelegate和UITableViewDelegate


3、initWithNibName是使用Xib文件的数据初始化控制器,参数应该是某一个xib文件对应的实体类


#import <UIKit/UIKit.h> #import <ReactiveObjC/ReactiveObjC.h> NS_ASSUME_NONNULL_BEGIN #pragma mark - Model @interface StoryScene : NSObject @property (nonatomic, copy) NSString *sceneTitle; @property (nonatomic, copy) NSString *visualDescription; @property (nonatomic, strong) NSArray<NSString *> *symbolicElements; @end @implementation StoryScene @end @interface DigitalTool : NSObject @property (nonatomic, copy) NSString *toolName; @property (nonatomic, copy) NSString *usageDescription; @property (nonatomic, strong) NSArray<NSString *> *appliedScenes; @end @implementation DigitalTool @end @interface InteractionPlan : NSObject @property (nonatomic, copy) NSString *platform; @property (nonatomic, strong) NSArray<NSString *> *engagementMethods; @end @implementation InteractionPlan @end #pragma mark - ViewModel @interface StoryViewModel : NSObject @property (nonatomic, strong) NSArray<StoryScene *> *scenes; @property (nonatomic, strong) RACCommand *loadScenesCommand; @end @implementation StoryViewModel - (instancetype)init { self = [super init]; if (self) { @weakify(self); _loadScenesCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id _) { @strongify(self); return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [self fetchScenesFromService]; [subscriber sendCompleted]; return nil; }]; }]; } return self; } - (void)fetchScenesFromService { StoryScene *scene1 = [StoryScene new]; scene1.sceneTitle = @"黑暗中的微光"; scene1.visualDescription = @"黑白基调牢房,霉变米饭特写"; StoryScene *scene2 = [StoryScene new]; scene2.sceneTitle = @"铁窗下的课堂"; scene2.symbolicElements = @[@"树枝笔", @"棉灰墨", @"半截铅笔"]; _scenes = @[scene1, scene2]; } @end @interface ToolViewModel : NSObject @property (nonatomic, strong) NSArray<DigitalTool *> *tools; @property (nonatomic, strong) RACSubject *toolSelectedSubject; @end @implementation ToolViewModel - (instancetype)init { self = [super init]; if (self) { _toolSelectedSubject = [RACSubject subject]; [self setupTools]; } return self; } - (void)setupTools { DigitalTool *tool1 = [DigitalTool new]; tool1.toolName = @"PikaLabs"; tool1.usageDescription = @"水墨晕染风格动画"; DigitalTool *tool2 = [DigitalTool new]; tool2.toolName = @"Runway Gen-3"; tool2.usageDescription = @"动态分镜特效"; _tools = @[tool1, tool2]; } @end #pragma mark - View @interface SceneCell : UICollectionViewCell @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UIImageView *symbolImageView; - (void)configureWithScene:(StoryScene *)scene; @end @implementation SceneCell - (void)configureWithScene:(StoryScene *)scene { _titleLabel.text = scene.sceneTitle; _symbolImageView.image = [UIImage imageNamed:scene.symbolicElements.firstObject]; } @end @interface MainViewController : UIViewController @property (nonatomic, strong) StoryViewModel *storyVM; @property (nonatomic, strong) ToolViewModel *toolVM; @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation MainViewController - (void)viewDidLoad { [super viewDidLoad]; [self setupCollectionView]; [self bindViewModels]; } - (void)setupCollectionView { UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new]; _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; [_collectionView registerClass:SceneCell.class forCellWithReuseIdentifier:@"SceneCell"]; [self.view addSubview:_collectionView]; } - (void)bindViewModels { @weakify(self); [[_storyVM.loadScenesCommand execute:nil] subscribeCompleted:^{ @strongify(self); [self.collectionView reloadData]; }]; [_toolVM.toolSelectedSubject subscribeNext:^(DigitalTool *tool) { NSLog(@"Selected tool: %@", tool.toolName); }]; } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _storyVM.scenes.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { SceneCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SceneCell" forIndexPath:indexPath]; [cell configureWithScene:_storyVM.scenes[indexPath.item]]; return cell; } @end #pragma mark - Coordinator @interface AppCoordinator : NSObject @property (nonatomic, strong) UINavigationController *navigationController; - (void)start; @end @implementation AppCoordinator - (void)start { MainViewController *mainVC = [MainViewController new]; mainVC.storyVM = [StoryViewModel new]; mainVC.toolVM = [ToolViewModel new]; _navigationController = [[UINavigationController alloc] initWithRootViewController:mainVC]; } @end #pragma mark - AppDelegate @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (nonatomic, strong) UIWindow *window; @property (nonatomic, strong) AppCoordinator *coordinator; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; _coordinator = [AppCoordinator new]; [_coordinator start]; _window.rootViewController = _coordinator.navigationController; [_window makeKeyAndVisible]; return YES; } @end NS_ASSUME_NONNULL_END
08-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值