【问题标题】:C4 navigating back and forth between subviewsC4 在子视图之间来回导航
【发布时间】:2013-10-23 11:38:17
【问题描述】:

我又觉得自己很傻,但我无法在项目的子视图之间来回导航。只要继续前进,我就可以添加任意数量的子视图,但是一旦我想回到以前访问过的视图之一,我就会遇到问题...... 这是我的简化代码:

C4Workspace.h

#import "C4CanvasController.h"
#import "FirstView.h"
@interface C4WorkSpace : C4CanvasController{
    FirstView *firstView;
}
@end

C4Workspace.m

#import "C4Workspace.h"

@implementation C4WorkSpace

-(void)setup {
    firstView=[FirstView new];
    firstView.canvas.frame=CGRectMake(0, 0, self.canvas.width, self.canvas.height);
    firstView.canvas.userInteractionEnabled=YES;
    [firstView setup];
    firstView.mainCanvas=self.canvas;
    [self.canvas addSubview:firstView.canvas];
}

@end

FirstView.h

#import "C4CanvasController.h"
#import "SecondView.h"

@interface FirstView : C4CanvasController{
    C4Label *goToSecondView;
    SecondView *secondView;

}
@property (readwrite, strong) C4Window *mainCanvas;

@end

FirstView.m

#import "FirstView.h"
@implementation FirstView
-(void)setup{
    goToSecondView=[C4Label labelWithText:@"go to second View"];
    goToSecondView.origin=CGPointMake(20, 50);
    [self.canvas addLabel:goToSecondView];
    [self listenFor:@"touchesBegan" fromObject:goToSecondView andRunMethod:@"goToSecondView"];
}
-(void)goToSecondView{
    [goToSecondView removeFromSuperview];
    C4Log(@"second view");
    secondView=[SecondView new];
    secondView.canvas.frame=CGRectMake(0, 0, self.canvas.width, self.canvas.height);
    secondView.canvas.userInteractionEnabled=YES;
    [secondView setup];
    secondView.mainCanvas=self.canvas;
    [self.canvas addSubview:secondView.canvas];  
}
@end

SecondView.h

#import "C4CanvasController.h"
#import "FirstView.h"
@interface SecondView : C4CanvasController{
    C4Label *goToFirstView;
    FirstView *firstView;
}
@property (readwrite, strong) C4Window *mainCanvas;
@end

SecondView.m

#import "SecondView.h"
@implementation SecondView
-(void)setup{
    goToFirstView=[C4Label labelWithText:@"go to First View"];
    goToFirstView.origin=CGPointMake(20, 50);
    [self.canvas addLabel:goToFirstView];
    [self listenFor:@"touchesBegan" fromObject:goToFirstView andRunMethod:@"goToFirstView"];
}

-(void)goToFirstView{
    [goToFirstView removeFromSuperview];
    C4Log(@"first view");
    firstview=[FirstView new];
    firstview.canvas.frame=CGRectMake(0, 0, self.canvas.width, self.canvas.height);
    firstView.canvas.userInteractionEnabled=YES;
    [firstView setup];
    firstView.mainCanvas=self.canvas;
    [self.canvas addSubview:firstView.canvas];   
}
@end

【问题讨论】:

    标签: ios navigation subviews c4


    【解决方案1】:

    我认为你的程序过于复杂了。

    哪个子视图的“控制”应该由包含视图控制器完成,因此,如果两个视图都在画布上,那么它应该是处理切换的画布而不是对象本身。

    有助于改进您的方法的事情:

    1. 从子类中删除对画布的引用。
    2. 子类控制器的视图都发布touchesBegan 通知...在工作区中使用这些,即[self listenFor:@"touchesBegan" fromObject:firstOrSecondView.canvas and runMethod:@"switch:"];
    3. 通过更改 zPosition 或隐藏/显示适当的视图进行切换...

    我将您的实现简化为以下内容:

    FirstView.m

    #import "FirstView.h"
    @implementation FirstView
    -(void)setup{
        goToSecondView=[C4Label labelWithText:@"go to second View"];
        goToSecondView.origin=CGPointMake(20, 50);
        [self.canvas addLabel:goToSecondView];
        goToSecondView.userInteractionEnabled = NO;
    }
    @end
    

    SecondView.m

    #import "SecondView.h"
    @implementation SecondView
    -(void)setup{
        goToFirstView=[C4Label labelWithText:@"go to First View"];
        goToFirstView.origin=CGPointMake(20, 50);
        [self.canvas addLabel:goToFirstView];
        goToFirstView.userInteractionEnabled = NO;
    }
    @end
    

    C4WorkSpace.m

    #import "C4Workspace.h"
    #import "FirstView.h"
    #import "SecondView.h"
    
    @implementation C4WorkSpace {
        FirstView *firstView;
        SecondView *secondView;
    }
    
    -(void)setup {
        firstView=[FirstView new];
        firstView.canvas.frame = self.canvas.frame;
        [firstView setup];
        firstView.canvas.userInteractionEnabled = YES;
        [self.canvas addSubview:firstView.canvas];
    
        secondView=[SecondView new];
        secondView.canvas.frame = self.canvas.frame;
        [secondView setup];
        secondView.canvas.userInteractionEnabled = YES;
        [self.canvas addSubview:secondView.canvas];
    
        secondView.canvas.hidden = YES;
    
        [self listenFor:@"touchesBegan"
            fromObjects:@[firstView.canvas, secondView.canvas]
           andRunMethod:@"switchView:"];
    }
    
    -(void)switchView:(NSNotification *)aNotification {
        C4View *v = (C4View *)aNotification.object;
    
        if([v isEqual:firstView.canvas]) {
            firstView.canvas.hidden = YES;
            secondView.canvas.hidden = NO;
        } else {
            firstView.canvas.hidden = NO;
            secondView.canvas.hidden = YES;
        }
    }
    
    @end
    

    如果您的视图需要不同的功能,这应该对您有用,例如 FirstView 将具有与 SecondView 显着不同的内部功能...如果它们相同,那么您应该考虑将它们折叠成相同的子类。

    【讨论】:

    • 是的,这可以正常工作,但现在用户可以在屏幕上的任何位置使用 tab 并进入另一个视图。我需要访问另一个画布的确切元素(因为该画布上发生了其他交互)然后切换视图。如果有办法这样做,那么这种方法会容易得多......
    • 关于不同观点的需要:我正在尝试重建一个新版本:vimeo.com/58437447。所以我在这里有一些完全不同的看法。我认为如果可以在不同的文件/视图中找到应用程序的不同状态,则更容易理解它们,对吧?
    • 现在自己回答我的问题:好的。因此,我认为如果我只希望该视图中的特定对象从一个视图更改为另一个视图,则在这些视图之间发布通知是一种方法……不知道以后共享该代码是否有意义。也许其他人也遇到过类似的问题?
    • 但还有一个问题:我有点担心应用程序的性能,因为我会有很多不同的观点。也许这与上述关于不同观点的评论有关:对所有不同的州有不同的观点是否有意义?这可能是性能与代码易读性的问题...
    • 不错的视频!我对你现在的情况有了更好的了解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    相关资源
    最近更新 更多