GVKun编程网logo

从 UIView 到 UIViewController?(uiview和uiviewcontroller)

31

在本文中,您将会了解到关于从UIView到UIViewController?的新资讯,同时我们还将为您解释uiview和uiviewcontroller的相关在本文中,我们将带你探索从UIView到U

在本文中,您将会了解到关于从 UIView 到 UIViewController?的新资讯,同时我们还将为您解释uiview和uiviewcontroller的相关在本文中,我们将带你探索从 UIView 到 UIViewController?的奥秘,分析uiview和uiviewcontroller的特点,并给出一些关于ios – UIViewController内的UITableViewController、ios – UIViewController和UITableViewController有什么区别?、ios – UIViewController应该控制UIView的动画吗?、ios – 与具有两个UINavigationController的rootViewController共享相同的UIViewController的实用技巧。

本文目录一览:

从 UIView 到 UIViewController?(uiview和uiviewcontroller)

从 UIView 到 UIViewController?(uiview和uiviewcontroller)

UIView有没有一种从
a到它的内置方法UIViewController?我知道你可以从UIViewControllerUIView通过[selfview]但我想知道是否有反向引用?

答案1

小编典典

由于长期以来这一直是公认的答案,我觉得我需要用更好的答案来纠正它。

关于需求的一些评论:

  • 您的视图不需要直接访问视图控制器。
  • 视图应该独立于视图控制器,并且能够在不同的上下文中工作。
  • 如果您需要视图以某种方式与视图控制器交互,推荐的方式以及 Apple 在 Cocoa 中所做的就是使用委托模式。

如何实现它的示例如下:

@protocol MyViewDelegate < NSObject >- (void)viewActionHappened;@end@interface MyView : UIView@property (nonatomic, assign) MyViewDelegate delegate;@end@interface MyViewController < MyViewDelegate >@end

视图与其委托接口(UITableView例如,如此),它不关心它是在视图控制器中实现还是在您最终使用的任何其他类中实现。

我的原始答案如下: 我不推荐这个,其他可以直接访问视图控制器的答案也不推荐

没有内置的方法可以做到这一点。虽然您可以通过在 Interface Builder
中添加和连接这些来解决它,但不建议这样做IBOutletUIView视图不应该知道视图控制器。相反,您应该按照@Phil M
的建议进行操作,并创建一个用作委托的协议。

ios – UIViewController内的UITableViewController

ios – UIViewController内的UITableViewController

我是iOS和Objective-C的新手,我在了解控制器的工作原理方面遇到麻烦.

在我的代码中,我有一个UIViewController(我的自定义控制器由storyboard分配),并在其内部,与其他对象一起,我想要一个不同的控制器处理的表.这样做的正确方法是什么?

解决方法

您可以将该表视图控制器设置为UIViewController的子视图控制器.在故事板中,您可以通过将容器视图拖动到控制器的视图中轻松实现,这将自动为您提供一个子视图控制器.你将要删除它给你的控制器(它只是一个UIViewController),拖出一个表视图控制器,控制从容器视图拖动到表视图控制器,并选择“嵌入”.如果您需要从UIViewController获取对此表视图控制器的引用,那么可以在prepareForSegue中执行此操作:表视图控制器将作为segue的目标视图控制器,并且将在控制器实例化之后调用prepareForSegue.

ios – UIViewController和UITableViewController有什么区别?

ios – UIViewController和UITableViewController有什么区别?

有时我想对一些应用程序广泛的自定义进行子类化UIViewController.例如.所有视图控制器应该在viewDidLoad或viewWillAppear中执行.

自然地,我将UIViewController子类化,并从那里继承,所有视图控制器都继承自此.但是一些控制器运行表.还有为此设计的UITableViewController.

所以我也分类UITableViewController,只是做同样的事情在那里.这似乎不是OOP术语中最聪明的事情.而且没有多重继承等

和UITableViewController继承自UIViewController …

现在我问自己为什么我不只是创建自己的表视图控制器,继承自己的视图控制器子类,并添加所有的表格.但是什么是“所有表的东西”?

> xcode添加到每个新的表视图控制器的框架代码.很方便,但可以轻松地转到代码段.
>有UITableViewDelegate和UITableViewDataSource协议的声明.随和.无论如何,这些方法的实现必须遵循UITableViewController的每个子类.
>协议中所有这些强制性方法可能有合理的默认实现.例如对于numberOfSectionsInTableView返回0或者对于titleForHeaderInSection返回0,或者对于heightForRowAtIndexPath,返回44.0f(不好的例子,可以更聪明地不执行)

所以尽管有明显的东西,UITableViewController有什么奇迹吗?

解决方法

我相信UITableViewController添加的所有行为在类文档: https://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewController_Class/Reference/Reference.html中都有很好的定义

The UITableViewController class creates a controller object that manages a table view. It implements the following behavior:

• If a nib file is specified via the initWithNibName:bundle: method (which is declared by the superclass UIViewController),UITableViewController loads the table view archived in the nib file. Otherwise,it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.

• If a nib file containing the table view is loaded,the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate,UITableViewController sets the data source and the delegate of the table view to self.

• When the table view is about to appear the first time it’s loaded,the table-view controller reloads the table view’s data. It also clears its selection (with or without animation,depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the cleaRSSelectionOnViewWillAppear property.

• When the table view has appeared,the controller flashes the table view’s scroll indicators. The UITableViewController class implements this in the superclass method viewDidAppear:.

• It implements the superclass method setEditing:animated: so that if a user taps an Edit|Done button in the navigation bar,the controller toggles the edit mode of the table.

所有这些都是行为,如果它们适用于您的特定控制器和表视图,应该很容易重新实现.

一般来说,我发现自己最好实现这些行为,以便允许替代继承层次结构,因为我注意到,因为我通常会考虑将表视图的delagate和datasource都设置为视图控制器的设计气味.这些是独立的关注,通常可以并且应该由其他类别处理(例如,用于特定模型类的专用数据源),而不是使视图控制器膨胀.

ios – UIViewController应该控制UIView的动画吗?

ios – UIViewController应该控制UIView的动画吗?

我一直在努力学习更多MVCS风格的编程,并试图更好地组织我的代码.我的主要问题是,我有一个UIViewController在事件发生时调出一个视图.当创建视图并且视图被销毁时,我想在视图上运行一些动画并且消失.我可以在UIView类和UIViewController中执行此操作.一旦建立了这些动画,就不需要更改它们.我应该在UIViewController或UIView中执行此操作以保持MVC兼容吗?

代码目前在我的UIView中:

- (IBAction)removeView
{
    NSLog(@"Remove");
    if (self.completionBlock != nil) {

        [UIView animateWithDuration:1.0f delay:0 options:UIViewAnimationoptionAllowUserInteraction animations:^{
            self.transform = CGAffineTransformMakeTranslation(self.frame.origin.x,self.frame.origin.y - self.superview.frame.size.height);
            self.alpha = 0; // also fade to transparent
        }completion:^(BOOL finished)
         {
             if (finished) {
                 [self removeFromSuperview];
             }
         }];

        self.completionBlock(YES);

    }
}

解决方法

根据您的描述,您可以在任何一个地方合法地执行它们,因为动画不依赖于您的应用程序的模型.但是,如果动画依赖于模型中的数据,则控制器应执行动画,因为控制器会看到模型数据(但视图不会).

ios – 与具有两个UINavigationController的rootViewController共享相同的UIViewController

ios – 与具有两个UINavigationController的rootViewController共享相同的UIViewController

更新:我已经决定与我的问题走一条不同的路线.
而不是直接使用相同的UIViewController,而是使用两个设置为rootViewController的vanilla UIViewController.在他们的loadView方法中,他们调用[storyboard instantiateViewControllerWithIdentifier:]来获取前一个UIViewController,并将其视图设置为控制器的视图.这可能是首选方法,因为我需要设置几个变量和代表.

我有一个UIStoryBoard与UITabBarController作为入口点与两个UINavigationControllers连接.每个共享一个共同的UIViewController作为其根视图控制器.当应用程序启动时,第一个UITabBarItem被选中,视图按预期方式加载.但是,当我选择第二个UITabBarItem时,相同的视图是不可见的.我看到带有黑色背景的UINavigationBar.我使用Storyboard界面做错了事情,还是需要通过每个UINavigationController的方法手动实例化UIViewController – 例如loadView?

解决方法

奇怪的是,这是一个没有其他人问的问题.据我所知,不可能共享rootViewController,我认为是一个错误,因为当您检查故事板上的连接时,您可以看到视图控制器连接到两个导航控制器.我认为这是故事板中的一个缺陷,因为重复的viewController和重新应用所有的连接是非常容易出错的,并使故事板过于复杂.

我看到你的解决方案的问题.像这样的解决方法让我有疑问,如果iOS中的当前故事板功能已准备好创建应用程序.我认为故事板有一个概念上的问题,苹果公司需要决定一个故事板上的一个viewController是否代表一个实例,或者它是否只代表该类,现在它不一致,因为您可以看到多个变量实际上可以指向相同的viewController,但实际上每个segue都有自己的实例,为什么还没有跟rootViewController连接?我不知道.

就像您的解决方案一样,请注意Apple的文档中的以下内容:

“重要视图控制器是其视图的唯一所有者及其创建的任何子视图,它负责创建这些视图,并在适当的时候放弃其所有权,包括在低内存条件下以及视图控制器本身被释放时如果您使用故事板或nib文件来存储视图对象,则视图控制器要求它们时,每个视图控制器对象会自动获取这些视图的自己的副本,但是如果手动创建视图,则不应使用具有多个视图控制器的相同视图对象“.

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html

今天关于从 UIView 到 UIViewController?uiview和uiviewcontroller的介绍到此结束,谢谢您的阅读,有关ios – UIViewController内的UITableViewController、ios – UIViewController和UITableViewController有什么区别?、ios – UIViewController应该控制UIView的动画吗?、ios – 与具有两个UINavigationController的rootViewController共享相同的UIViewController等更多相关知识的信息可以在本站进行查询。

本文标签: