首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C4在子视图之间来回导航

C4在子视图之间来回导航
EN

Stack Overflow用户
提问于 2013-10-23 11:38:17
回答 1查看 71关注 0票数 1

我再次感到很傻,但我无法在我的项目的子视图之间来回导航。只要继续,我可以添加任意多个子视图,但只要我想回到以前访问过的视图之一,我就有一个问题.以下是我的简化代码:

C4Workspace.h

代码语言:javascript
复制
#import "C4CanvasController.h"
#import "FirstView.h"
@interface C4WorkSpace : C4CanvasController{
    FirstView *firstView;
}
@end

C4Workspace.m

代码语言:javascript
复制
#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

代码语言:javascript
复制
#import "C4CanvasController.h"
#import "SecondView.h"

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

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

@end

FirstView.m

代码语言:javascript
复制
#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

代码语言:javascript
复制
#import "C4CanvasController.h"
#import "FirstView.h"
@interface SecondView : C4CanvasController{
    C4Label *goToFirstView;
    FirstView *firstView;
}
@property (readwrite, strong) C4Window *mainCanvas;
@end

SecondView.m

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-23 19:32:57

我觉得你已经把手术复杂化了。

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

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

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

我将您的实现简化为:

FirstView.m

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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有很大的不同,这应该适用于您.如果它们是相同的,那么您应该考虑将它们折叠到同一个子类中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19540590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档