-1

好吧,我的错误,我之前认为我的 ivar 没有正确更改。我错了(并且得到了一些反对票)。抱歉,真正的问题是我一直在错误地调用它。我现在发布所有代码,我会让你看一下。

我的第一堂课:

类一.h:

@interface DetailPageController : UIPageViewController
{

    BOOL isChromeHidden_;

}

- (void)toggleChromeDisplay;


@end

classOne.m:

@interface DetailPageController ()

@end

@implementation DetailPageController

- (void)toggleChromeDisplay
{
    [self toggleChrome:!isChromeHidden_];
}

- (void)toggleChrome:(BOOL)hide
{
    //Find chrome value
    isChromeHidden_ = !isChromeHidden_;
    NSLog(isChromeHidden_ ? @"YES" : @"NO");     

}

@end

从我收到的评论来看,我相信这些都不是真正的问题,它在下面。

classTwo.h:(没有声明)

类二.m:

@interface classTwo ()


@end

@implementation classTwo

//Touches Control
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    if ([touch view]) {
        if ([touch tapCount] == 2) {
            NSLog(@"double touched");
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(toggleChromeDisplay) object:nil];
        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    if ([touch view]) {
        if ([touch tapCount] == 1) {
            NSLog(@"single touch");
            [self performSelector:@selector(toggleChromeDisplay) withObject:nil afterDelay:0.5];
        }
    }
}

- (void)toggleChromeDisplay
{

    DetailPageController *pageController = [[DetailPageController alloc] init];
    [pageController toggleChromeDisplay];
}

@end

再次,对上一篇文章感到抱歉,我曾认为这是该方法的问题,但实际上是我所说的方式。

我被迫做的是在处理触摸区域的控制器中实现触摸,但我在另一个中有用于 chrome(导航栏和工具栏)的方法。

总体问题 为什么每次我在 classTwo 中调用 toggleChromeDisplay 方法时,我总是从 classOne 中的 ivar 中得到相同的 NO?

我为 classTwo.h 尝试过的内容:

#import "DetailPageController.h"

@interface classTwo : UIViewController
{
    DetailPageController *detailPageController_;
}

@property (nonatomic, assign) DetailPageController *detailPageController;

@end

我修改后的代码:

在我的 classTwo.h 中:

#import "DetailPageController.h"

@interface PhotoViewController : UIViewController
{
    DetailPageController *detailPageController_;
}



@property (nonatomic, strong) DetailPageController *detailPageController;

@end

类二.m:

#import "classTwo.h"

@interface classTwo ()

@end

@implementation classTwo

@synthesize detailPageController = detailPageController_;

//Touches Control
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    if ([touch view]) {
        if ([touch tapCount] == 2) {
            NSLog(@"double touched");
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(toggleChromeDisplay) object:nil];
        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    if ([touch view]) {
        if ([touch tapCount] == 1) {
            NSLog(@"single touch");
            [self performSelector:@selector(toggleChromeDisplay) withObject:nil afterDelay:0];
        }
    }
}

- (void)toggleChromeDisplay
{
    [self.detailPageController toggleChromeDisplay];
}

@end
4

4 回答 4

2

所有你需要的是

- (void)toggleChromeDisplay
{
    isChromeHidden_ = !isChromeHidden_;
}

(根据您的编辑)

于 2013-08-12T01:53:57.970 回答
2

classTwo always instantiates a completely brand new instance of DetailPageViewController before calling toggleChromeDisplay on it, and then it allows it to go out of scope and presumably is deallocated. You need to have it use an instance that's actually alive in your program and controls views in the view hierarchy.

You could add a DetailPageViewController property to classTwo and then your -[classTwo toggleChromeDisplay] implementation would look like:

- (void)toggleChromeDisplay
{
    [self.detailPageController toggleChromeDisplay];
}

Again, just make sure to assign to that property the instance of the view controller that's actually on screen. alloc and init are used to creating brand new instances of objects, which won't be the ones that already exist in your application if they've been loaded up, for instance, from a storyboard. So, in your application you probably have a DetailPageViewController instance already that is doing things on the screen and controlling interactions with the user - but your classTwo never is able to message it because again it's creating entirely separate instances of that class. So you need to determine where in your application the DetailPageViewController that's visible on screen is getting instantiated, and at that point ensure your classTwo instance can get a reference to it.

Forgive my repetitiveness, but it is a common mistake I see on Stack Overflow. Just make sure that you understand that while there is one definition of a Class, which is where its instance variables and methods are defined, there can be many separate instances of objects that are created from it (we often say they are instantiated, or created, inited, you'll see a number of terms). Each of those objects can have different values for their instance variables (and properties), and they all have their own distinct life time from a memory-management standpoint. Calling the pair of methods alloc and init is one very common way to make a new instance of a class, that has its own lifetime and instance variables.

Finally, I'd like to suggest that you read and follow Apple's Cocoa style guide as your choice of names for methods and classes has caused confusion among your fellow developers. If you start to apply that you will find communication with others to go smoother and your problems easier to understand.

于 2013-08-12T02:49:23.727 回答
1

toggleChrome:设置变量,isChromeHidden_它不切换它。要切换你会写:

- (void)toggleChrome
{
    isChromeHidden_ = !isChromeHidden_;

    //Find chrome value
    NSLog(isChromeHidden_ ? @"YES" : @"NO");
}

!isChromeHidden_​​ 是 isChromeHidden_​​ 的相反值。

于 2013-08-12T01:27:28.667 回答
1

我将您的确切代码复制到我的项目中,它按预期在 YES 和 NO 之间切换。这告诉我您发布的代码实际上是正确的,您要么错误地调用它,要么您也在其他地方设置变量。

于 2013-08-12T02:04:19.093 回答