访问输出的“NSInteger”在我的其余代码中

发布于 2024-12-28 22:11:45 字数 780 浏览 3 评论 0原文

我正在尝试做一些我认为相对简单的事情,获取一周中的当前日期,然后在点击按钮时针对该天进行一些检查。为了获取星期几,我在 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。我确信我错过了一些非常简单的东西。有什么建议吗?

I am trying to do something I thought would be relatively simple and get the current day of the week, then make some checks against that day when buttons are tapped. to get the day of the week I have used the following code in my viewDidLoad method:

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];

The code returns me two NSIntegers which are the current day of the week and the current day of the month, which is what I wanted. I now want to have a series of buttons, which when tapped displays a certain webpage in a UIWebView depending on the day of the week.

I was hoping to do a simple:

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

But I can't seem to get access to the 'weekday' NSInteger which was created in the viewDidLoad method. Im sure I am missing something very simple. Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风和你 2025-01-04 22:11:45

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

@property (nonatomic, retain) NSInteger weekday;

然后在实现中使用 @synthesize

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

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

The problem is weekday is out of scope when you try to use it in the btnClicked method. Try creating a property for it in the interface section like this:

@property (nonatomic, retain) NSInteger weekday;

Then @synthesize in the implementation

Or alternatively just add it as an instance variable:

@interface class : NSObject {
    NSInteger weekday;
}
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文