Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何更改局部类变量IBAction_Objective C_Cocoa_Ibaction - Fatal编程技术网

Objective c 如何更改局部类变量IBAction

Objective c 如何更改局部类变量IBAction,objective-c,cocoa,ibaction,Objective C,Cocoa,Ibaction,各位!! 如果有人能帮助我,我将非常高兴^ 问题是: 我在类中单击了iAction方法复选框,我想通过将复选框单击为粗体状态来更改字体属性。 我试图在IBAction方法中更改局部类变量,以便在我的-drawRect:方法中检查它,因为它不起作用-变量不会更改。 如何在iAction方法中更改局部变量,或者有其他方法? 谢谢 但这里的isBold仍然是“否” - (void)drawRect:(NSRect)rect { NSRect bounds = [self bounds]; [bgCo

各位!! 如果有人能帮助我,我将非常高兴^

问题是: 我在类中单击了iAction方法复选框,我想通过将复选框单击为粗体状态来更改字体属性。 我试图在IBAction方法中更改局部类变量,以便在我的-drawRect:方法中检查它,因为它不起作用-变量不会更改。 如何在iAction方法中更改局部变量,或者有其他方法? 谢谢

但这里的isBold仍然是“否”

- (void)drawRect:(NSRect)rect {
NSRect bounds = [self bounds];
[bgColor set];
[NSBezierPath fillRect:bounds];
[self drawStringCenteredIn:bounds];
NSLog(@"isBold: %d",isBold); //HERE prints 'isBold:0'
if (isBold == YES) { 
NSFont *aFont = [attributes objectForKey:NSFontAttributeName];
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[attributes setObject:[fontManager convertFont:aFont toHaveTrait:NSBoldFontMask] forKey:NSFontAttributeName];
}

//whether this view is firstResponder
if ([[self window] firstResponder] == self && 
[NSGraphicsContext currentContextDrawingToScreen]) {
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[NSBezierPath fillRect:bounds];
[NSGraphicsContext restoreGraphicsState];

}
} // drawRect

顺便说一句,我正在做亚伦·希勒加斯的书中的第20章。

到目前为止,你所展示的看起来不错,但显然不起作用,否则你就不会在这里了。尝试将此行添加到-makeBold:方法的末尾:


这将迫使视图重新绘制自己;您的问题可能只是视图没有被重新绘制。

请向我们展示代码,特别是如何确保确实调用了操作方法。感谢您的评论,Ole!完成了。谢谢你的回答,杰夫!一分钟前就试过了没有什么变化。问题是我试图在iAction中更改变量,然后在另一个方法中检查这个变量值。iAction i打印日志中的变量更改,但另一个方法中的变量的旧值更改。看起来iAction中的变量和类的其余部分有另一个内存空间..如何创建视图?首先,我创建了一个从NSView继承的类。然后我将IB中的“自定义视图”和类标识集默认NSView拖到以前创建的类中。我今天得到了帮助。问题出在我的.xib文件中——我与类的另一个多余变量建立了连接,而不是与NSView的继承对象进行连接。不过还是要谢谢你!!
- (IBAction)makeBold:(id)sender
{
if ([boldButton state] == NSOnState) {
isBold = YES;
NSLog(@"Action bold=%d", isBold);
}
else {
isBold = NO;
NSLog(@"Action bold=%d", isBold);
}

}
- (void)drawRect:(NSRect)rect {
NSRect bounds = [self bounds];
[bgColor set];
[NSBezierPath fillRect:bounds];
[self drawStringCenteredIn:bounds];
NSLog(@"isBold: %d",isBold); //HERE prints 'isBold:0'
if (isBold == YES) { 
NSFont *aFont = [attributes objectForKey:NSFontAttributeName];
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[attributes setObject:[fontManager convertFont:aFont toHaveTrait:NSBoldFontMask] forKey:NSFontAttributeName];
}

//whether this view is firstResponder
if ([[self window] firstResponder] == self && 
[NSGraphicsContext currentContextDrawingToScreen]) {
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[NSBezierPath fillRect:bounds];
[NSGraphicsContext restoreGraphicsState];

}
} // drawRect
[self setNeedsDisplay];