gpt4 book ai didi

iphone - 如何在调用 objective-c 方法之前设置NSString

转载 作者:行者123 更新时间:2023-11-29 04:00:24 27 4
gpt4 key购买 nike

这可能是一个简单的问题,但我无法弄清楚我错过了什么。

在 ViewControl.h 中我声明了 UIColor

@property (nonatomic, strong) UIColor * myColor;

在 ViewControl.m 中,我有一个方法可以执行某些操作并返回新的 UIColor

@synthesize myColor = _myColor;

在ViewDidLoad方法中

- (void)viewDidLoad
{
myColor = [UIColor RedColor];
}

-(void) ShowColorPopUpView
{
if (!self.wePopoverController)
{

ColorViewController *contentViewController = [[ColorViewController alloc] init];
contentViewController.delegate = self;
self.wePopoverController = [[WEPopoverController alloc] initWithContentViewController:contentViewController];
self.wePopoverController.delegate = self;
self.wePopoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar];

[self.wePopoverController presentPopoverFromRect:self.tvTweetDetails.frame
inView:self.view
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown)
animated:YES];

} else
{
[self.wePopoverController dismissPopoverAnimated:YES];
self.wePopoverController = nil;
}
}

-(void) colorPopoverControllerDidSelectColor:(NSString *)hexColor
{
_myColor = [GzColors colorFromHex:hexColor];
[self.view setNeedsDisplay];
[self.wePopoverController dismissPopoverAnimated:YES];
self.wePopoverController = nil;
}
- (UIColor *) returnColor
{
return _myColor;
}

我的问题从这里开始:我有两种方法来更改 TextView 字体和背景颜色

- (IBAction)btnFontColorPopUpMenu:(id)sender
{
[self ShowColorPopUpView];
tvTweetDetails.textColor = [self returnColor];
}
- (IBAction)btnTextViewBackGroundColor:(id)sender
{
[self ShowColorPopUpView];
tvTweetDetails.backgroundColor = [self returnColor];
}

现在的问题是,当我调用该方法时,它会返回 RED,如果我再次调用它,它会返回 BlackColor。

如何调用该方法并将颜色更改为新颜色,然后返回它。我想直接获得黑色。

我想先执行该方法,然后返回颜色,但发生的情况是在执行该方法之前分配颜色。

我希望我已经把问题说清楚了。

最佳答案

好的,我会努力解决这个问题。

我怀疑你正在做某种presentViewController: ...换色器方法中的方法。这很好,但它也有影响。您在演示期间调用的方法将继续执行。这意味着它可能会返回,等等。

这就是委托(delegate)概念的用武之地。您可能会从这里稍微重构数据流中受益。

我建议(如果我对颜色选择器 UI 的呈现是正确的)进行以下更改:

  1. 创建 @protocol ColorPickerDelegate使用一种方法:-(void) userChoseColor:(UIColor *) color
  2. 添加@property (weak) id<ColorPickerDelegate> delegate到您的颜色选择器 View Controller
  3. 让您的 VC 实现该协议(protocol)
  4. 在委托(delegate)方法内,设置本地属性
  5. 为本地属性实现自定义 setter ,并在颜色发生变化时更新背景颜色。

关于iphone - 如何在调用 objective-c 方法之前设置NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15931155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com