gpt4 book ai didi

iphone - 在我的代码的其余部分中访问输出的 'NSInteger'

转载 作者:行者123 更新时间:2023-12-03 20:45:02 27 4
gpt4 key购买 nike

我正在尝试做一些我认为相对简单的事情并获取本周的当前日期,然后在点击按钮时针对该天进行一些检查。为了获取星期几,我在 viewDidLoad 方法中使用了以下代码:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];

代码返回两个 NSIntegers,分别是本周的当前日期和本月的当前日期,这就是我想要的。我现在想要一系列按钮,点击这些按钮时会根据星期几在 UIWebView 中显示某个网页。

我希望做一个简单的:

-(IBAction) btnClicked 
{
if (weekday == 1)
{
//DO SOMETHING HERE
{
}

但我似乎无法访问在 viewDidLoad 方法中创建的“工作日”NSInteger。我确信我错过了一些非常简单的东西。有什么建议吗?

最佳答案

问题是当您尝试在 btnClicked 方法中使用工作日时,它超出了范围。尝试在界面部分为其创建一个属性,如下所示:

@property (nonatomic, retain) NSInteger weekday;

然后在实现中使用@synthesize

或者只是将其添加为实例变量:

@interface class : NSObject {
NSInteger weekday;
}
@end

关于iphone - 在我的代码的其余部分中访问输出的 'NSInteger',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8996510/

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