访问输出的“NSInteger”在我的其余代码中
我正在尝试做一些我认为相对简单的事情,获取一周中的当前日期,然后在点击按钮时针对该天进行一些检查。为了获取星期几,我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是当您尝试在 btnClicked 方法中使用工作日时,它超出了范围。尝试在接口部分为其创建一个属性,如下所示:
然后在实现中使用 @synthesize
或者将其添加为实例变量:
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:
Then @synthesize in the implementation
Or alternatively just add it as an instance variable: