相关疑难解决方法(0)

使用xib创建可重用的UIView(并从storyboard加载)

好的,StackOverflow上有很多关于此的帖子,但没有一个在解决方案上特别清楚.我想UIView用随附的xib文件创建一个自定义.要求是:

  • 没有单独的UIViewController- 一个完全独立的类
  • 类中的出口允许我设置/获取视图的属性

我目前的做法是:

  1. 覆盖 -(id)initWithFrame:

    -(id)initWithFrame:(CGRect)frame {
        self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                              owner:self
                                            options:nil] objectAtIndex:0];
        self.frame = frame;
        return self;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. -(id)initWithFrame:在我的视图控制器中以编程方式实例化

    MyCustomView *myCustomView = [[MyCustomView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self.view insertSubview:myCustomView atIndex:0];
    
    Run Code Online (Sandbox Code Playgroud)

这工作正常(虽然从来没有调用[super init],只是使用加载的笔尖的内容设置对象似乎有点怀疑 - 这里有建议添加一个子视图在这种情况下也工作正常).但是,我希望能够从故事板中实例化视图.所以我可以:

  1. UIView在故事板中放置父视图
  2. 将其自定义类设置为 MyCustomView
  3. 覆盖-(id)initWithCoder:- 我见过的代码最常适合以下模式:

    -(id)initWithCoder:(NSCoder *)aDecoder {
        self = [super initWithCoder:aDecoder];
        if (self) {
            [self initializeSubviews];
        }
        return self;
    }
    
    -(id)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame]; …
    Run Code Online (Sandbox Code Playgroud)

objective-c storyboard uiview nib ios

79
推荐指数
5
解决办法
6万
查看次数

如何在Objective-C中添加具有自己的UIViewController的子视图?

我正在为拥有自己的子视图而苦苦挣扎UIViewControllers.我有一个UIViewController视图(浅粉色)和两个按钮toolbar.我希望在按下第一个按钮时显示蓝色视图,按下第二个按钮时显示的黄色视图.如果我只是想显示一个视图,应该很容易.但蓝色视图将包含一个表,因此它需要它自己的控制器.那是我的第一课.我从这个SO问题开始,我知道我需要一个控制器用于表.

所以,我要支持并采取一些婴儿步骤.下面是我的Utility ViewController(主视图控制器)和另外两个控制器(蓝色和黄色)的简单起点图片.想象一下,当ViewController首次显示Utility (主视图)时,将显示粉红色视图所在的蓝色(默认)视图.用户可以单击两个按钮来回移动,粉红色视图将永远不会显示.我只想将蓝色视图移到粉红色视图的位置,将黄色视图移到粉红色视图所在的位置.我希望这是有道理的.

简单的故事板图像

我正在尝试使用addChildViewController.从我所看到的,有两种方法可以做到这一点:容器视图storyboardaddChildViewController编程.我想以编程方式进行.我不想使用NavigationController或Tab栏.我只想添加控制器,并在按下相关按钮时将正确的视图推入粉红色视图.

下面是我到目前为止的代码.我想要做的就是显示粉红色视图所在的蓝色视图.从我所看到的,我应该能够addChildViewController和addSubView.这段代码不适合我.我的困惑是让我变得更好.有人可以帮我看到粉红色视图所在的蓝色视图吗?

除了在viewDidLoad中显示蓝色视图外,此代码不用于执行任何操作.

IDUtilityViewController.h

#import <UIKit/UIKit.h>

@interface IDUtilityViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *utilityView;
@end
Run Code Online (Sandbox Code Playgroud)

IDUtilityViewController.m

#import "IDUtilityViewController.h"
#import "IDAboutViewController.h"

@interface IDUtilityViewController ()
@property (nonatomic, strong) IDAboutViewController *aboutVC;
@end

@implementation IDUtilityViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.aboutVC = [[IDAboutViewController alloc]initWithNibName:@"AboutVC" bundle:nil];
    [self addChildViewController:self.aboutVC];
    [self.aboutVC didMoveToParentViewController:self];
    [self.utilityView addSubview:self.aboutVC.aboutView];
}

@end
Run Code Online (Sandbox Code Playgroud)

- - - - …

objective-c uitableview uiviewcontroller uicontainerview

53
推荐指数
2
解决办法
3万
查看次数

IOS8如何移动一个活跃的popover

我为iOS7开发了一个应用程序,现在尝试为iOS8更新它.问题我有以下几点:

应用程序屏幕方向可以旋转,在某些情况下,一些按钮会大幅移动.我有一些指向这些按钮的弹出窗口,所以如果在屏幕旋转时弹出窗口,按钮移动,我也需要弹出窗口.

在iOS7中我通过以下方式做到了这一点:当屏幕旋转时我更新了约束

- (void) updateViewConstraints 
{
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
    {
        self.Button.constant = (CGFloat)10;
    }
    else
    {
        self.Button.constant = (CGFloat)5;
    }
    [super updateViewConstraints];
} 
Run Code Online (Sandbox Code Playgroud)

我也移动了弹出窗口

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{

    if(TempDisplayPopoverController == examplePopoverController)
    {
        [examplePopoverController presentPopoverFromRect:[self ExamplePopoverPosition] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}
Run Code Online (Sandbox Code Playgroud)

我最初加载弹出窗口

- (void) LoadPopover{
    examplePopover = [[examplep alloc] initWithNibName:@"exampleP" bundle:nil];
    [examplePopover setDelegate:self];
    examplePopoverController = [[UIPopoverController alloc] initWithContentViewController: examplePopover];
    [examplePopoverController setDelegate:self];

    examplePopoverController.popoverContentSize = examplePopover.view.frame.size;

    TempDisplayPopoverController = examplePopoverController;


    if ([examplePopoverController isPopoverVisible])
    {
        [examplePopoverController dismissPopoverAnimated:YES];
    }
    else
    {
        [examplePopoverController presentPopoverFromRect:[self ExamplePopoverPosition] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; …
Run Code Online (Sandbox Code Playgroud)

objective-c screen-orientation uipopover

8
推荐指数
1
解决办法
3168
查看次数

如何为 TableView 和 CollectionView 制作可重用的单元格

我在不同的 ViewController 中有一个 TableView 和一个 CollectionView。现在我在它们两个中显示完全相同的单元格。所以我想创建一个可重复使用的单元,以方便和易于维护。

我尝试创建一个 xib 并将自定义类设置为一个UITableViewCell类,然后我可以在UITableView. 但是,UICollectionView由于 CollectionView 无法加载 TableViewCell ,因此我无法在其中重用此 xib 。

所以我的问题是有什么好的方法可以为 TableView 和 CollectionView 制作可重用的单元格?

xib ios

6
推荐指数
1
解决办法
2243
查看次数

自定义UIView子类要求两次调用awakeFromNIB

我绝望地被困住了,我会很感激指针.

我们正在尝试构建一个包含多个视图的视图控制器,这些视图是UIView的子类.除了我们需要手动初始化视图或再次手动调用awakeFromNib(这是不允许的)之外,一切都工作得很好.

这是问题......

Subclassed UIView头文件:

#import <UIKit/UIKit.h>
@interface introView : UIView
@property (strong, nonatomic) UIView *view;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@end
Run Code Online (Sandbox Code Playgroud)

Subclassed UiView主文件:

#import "introView.h"

@interface introView ()
@end

@implementation introView

-(id)init
{
    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"introView" owner:self options:nil];
    id mainView = [subviewArray objectAtIndex:0];
    return mainView;
}
- (void) awakeFromNib
{
    [super awakeFromNib];
    [self initialize];
    [self addSubview:self.view];
}

-(void)initialize
{
    NSLog(@"title: %@", self.title);
    self.titleLabel.text = self.title;
}
Run Code Online (Sandbox Code Playgroud)

然后我们从视图控制器初始化视图:

introView *view = …
Run Code Online (Sandbox Code Playgroud)

objective-c uiview ios

5
推荐指数
1
解决办法
9037
查看次数