简体   繁体   English

如何在UIViewController内部的UIView之间交换?

[英]How to swap between UIViews inside a UIViewController?

I have created 8 UIViews , I have a UIPicker and when the user selects something with the UIPicker I run the thread though a big if statement which I am not happy about. 我已经创建了8个UIViews ,我有一个UIPicker ,当用户使用UIPicker选择某些内容时,我会通过一个令我不满意的大if语句运行线程。

I would like to know if what I am doing is okay? 我想知道我在做什么吗? and also if the code I am using to load the view is how it should be done.. the code is below you will see I am simply loading the UIView once, then bringing it to the front layer... I would rather reload it entirely using remove from subview... but I couldn't get that to work.. 并且如果我用来加载视图的代码是应该怎么做的..下面的代码您将看到我只加载一次UIView ,然后将其带到顶层...我宁愿重新加载它完全使用从子视图中删除...但是我无法正常工作..

Any one of the UIViews can be loaded at one time which is what is causing me issue. 一次可以加载任何UIViews ,这是导致我出现问题的原因。

#pragma mark - Picker Delegates
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // ReloadView
    if (row == 0) {
        if (my1View == nil ) {
            [self DrawView1];
        }
        [self.view bringSubviewToFront:my1View];
    } else if (row == 1) {
        if (my2View == nil ) {
            [self DrawView2];
        }
        [self.view bringSubviewToFront:my2View];
    }else if (row == 2) {
        if (my3View == nil ) {
            [self DrawView3];
        }
        [self.view bringSubviewToFront:my3View];
    }else if (row == 3) {
        if (my4View == nil ) {
            [self DrawView4];
        }
        [self.view bringSubviewToFront:my4View];
    }else if (row == 4) {
        if (my5View == nil ) {
            [self DrawView5];
        }
        [self.view bringSubviewToFront:my5View];
    }else if (row == 5) {
        if (my6View == nil ) {
            [self DrawView6];
        }
        [self.view bringSubviewToFront:my6View];
    }else if (row == 6) {
        if (my7View == nil ) {
            [self DrawView7];
        }
        [self.view bringSubviewToFront:my7View];
    }else if (row == 7) {
        if (my8View == nil ) {
            [self DrawView8];
        }
        [self.view bringSubviewToFront:my8View];
    }
}

The only other issue is that sometimes it will get stuck on one view when no matter what I pick nothing will load over it. 唯一的另一个问题是,有时无论我选择什么都不会加载它,有时它将卡在一个视图上。

First of all, maybe to make it more beautiful you could think about a switch statement, but in terms of performance this doesn't matter: 首先,也许为了使其更漂亮,您可以考虑使用switch语句,但是就性能而言,这并不重要:

switch (row) {
        case 0:
            if (my1View == nil ) {
                [self DrawView1];
            }
            [self.view bringSubviewToFront:my1View];
            break;
        case 1:
            if (my1View == nil ) {
                [self DrawView2];
            }
            [self.view bringSubviewToFront:my2View];
            break;

           //THE OTHER CASES HERE...

        default:
            break;
}

About the rest: I would suggest you put all the views into an array or dictionary and also have a variable currentView 关于其余内容:我建议您将所有视图放入数组或字典中,并使用变量currentView

@property (nonatomic, strong) UIView* currentView;
@property (nonatomic, strong) NSMutableDictionary* myViews;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // ReloadView
    UIView* myNewView =
    [self.myViews objectForKey:[NSString stringWithFormat:@"%d",(int)row]];

    if (myNewView!=self.currentView) {
        [self.currentView removeFromSuperview];
    }
    if (!myNewView) {
        [self drawView:row];
    }
    [self.view addSubview:myNewView];
    self.currentView = myNewView;
}

Make sure to initiate the myViews property in your viewDidLoad (or something similar) like this: 确保像这样在viewDidLoad(或类似的东西)中启动myViews属性:

self.myViews = [NSMutableDisctionary new];

Also you need a new method for this row: [self drawView:row]; 您还需要为此行提供一个新方法: [self drawView:row]; , in this one you can just call your existing methods or copy your existing methods into this one! ,在此方法中,您只需调用现有方法或将现有方法复制到此方法中即可!

In order to change as little as possible, you can use: 为了使更改尽可能少,您可以使用:

- (void)drawView:(NSInteger)row
{
    switch (row) {
        case 0:
            [self DrawView1];
            break;
        case 1:
            [self DrawView2];
            break;

            //THE OTHER CASES HERE...

        default:
            break;
    }
}

I have an app that has a place-holder UIView (self.mainView) for viewControllers to be placed based on a tab selection. 我有一个具有占位符UIView(self.mainView)的应用程序,用于根据选项卡选择放置viewControllers。 Using this technique is good because then only 1 of the views is loaded at any time, and I can create a ViewController class that operates with that view instead of having a monolithic controller that needs to understand all the views that may be in that place. 使用此技术是件好事,因为此时可以仅加载其中一个视图,并且我可以创建一个与该视图一起操作的ViewController类,而不是使用需要理解该位置所有视图的单片式控制器。

Here is some sample code I use to show the appropriate viewController into that place holder. 这是一些示例代码,用于将适当的viewController显示到该占位符中。

@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (strong) UIViewController *currentController;

if (self.currentController) {
    [self.currentController willMoveToParentViewController:nil];
    [[self.currentController view] removeFromSuperview];
    [self.currentController removeFromParentViewController];
    self.currentController = nil;
}


switch (row) {
    case 0:
        self.currentController = [self.storyboard instantiateViewControllerWithIdentifier:@"AboutViewController"];
        break;

    case 1: {
        ...
    }
        break;
}


if (self.currentController) {
    [self addChildViewController:self.currentController];
    [self.mainView addSubview:self.currentController.view];
    [self.currentController didMoveToParentViewController:self];
    [self adjustMainView]; // my own method to adjust some constraints base on flags
    [self.view layoutIfNeeded];
}

I developer your project and I can't see a problem in your code. 我开发了您的项目,但是在您的代码中看不到问题。 Works fine. 工作正常。 There is no problem in the code that you are showing. 您显示的代码没有问题。 Maybe you should share more code to check it. 也许您应该共享更多代码进行检查。

I tried to follow your style, however I changed small things. 我尝试遵循您的风格,但是我改变了一些小东西。

You can found the project I created to test your code here . 您可以在此处找到我创建的用于测试代码的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当单击 Swift 中 NSObject 内部的 tableView 中的项目时,如何在 NSObject Class 内部的 UIViews 或 UIViewController 之间正确切换? - How to switch properly between UIViews or UIViewController inside NSObject Class when clicked the item in tableView inside of NSObject in Swift? 将UIViews移到另一个UIViewController中 - Move UIViews inside another UIViewController 如何从UIViewController中删除外部UIViews? - How to remove outside UIViews from UIViewController? 如何在一个 UIViewController 中控制多个相同的 UIView - How to control several same UIViews in one UIViewController 如何检测2个UIView之间的碰撞 - How to detect Collision between 2 UIViews 在UIViewController(iOS 7)中“堆叠”UIViews - “Stack” UIViews within a UIViewController (iOS 7) 为在UIViewController中找到的UIView创建控制器 - Creating Controllers for the UIViews found in a UIViewController 将 UIViews 或结构数组传递给 UIViewController - Passing array of UIViews or structs to UIViewController Swift:如何在 UIViewControllers 中安排 UIViews? - Swift: How to arrange UIViews inside UIViewControllers? 如何在许多级别的UIView中搜索UIView? - How to search for UIView inside many levels of UIViews?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM