我需要将NSDate设置为iPhone App的特定日期,时间和时区.
下面的示例代码在iOS 4上运行正常,因为在iOS 4中引入了setTimeZone.如何在不使用setTimeZone的情况下轻松地将NSDate设置为日期,时间和时区,以便可以在iOS 3.0设备上使用?
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:2010];
[comps setMonth:8];
[comps setDay:24];
[comps setHour:17];
[comps setMinute:5];
[comps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"] ];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *referenceTime = [cal dateFromComponents:comps];
Run Code Online (Sandbox Code Playgroud)
猜猜这应该很简单,但不知何故NSDate,组件和日历和我不是好朋友......
我刚开始与计时器合作,iPhone需要一些支持.
我有一个方法如下所示,花费时间并更新UILabel我的userinterface.我也有一个NSTimer调用该方法一次一次(我只显示小时和分钟).这个工作得很好,除了应用程序的第一秒实时(据我所知,在timer调用方法之前需要一秒钟).
我想调用我的方法viewDidLoad,但我怎样才能提供正确的参数?或者有更好的方法吗?
// Timer for updating time
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(updateTime:)
userInfo:nil
repeats:YES];
-(void)updateTime: (NSTimer *) timer {
currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTimeString = [dateFormatter stringFromDate:currentTime];
[dateFormatter release];
timeLabel.text = currentTimeString;
}
Run Code Online (Sandbox Code Playgroud)
欣赏任何可以指向正确方向的东西.
我想知道是否有人遇到过同样的问题以及他们是如何解决的.
我想本地化Default.png所以我做了以下步骤,从我的理解应该是正确的方法(如果我错了请纠正我).
我已经在模拟器和iPhone上试过改变英语和西班牙语之间的语言,但我只得到原始文件.应用程序名称和字符串等其他本地化工作正常,但不是这个...
我在xcode 3.2.3上
谢谢
仍在学习Objective C并使结构正确.
我有一个带有UIViewController的iOS应用程序,它有一个名为"doSomething"的已定义方法.在我的视图控制器中,我有一个视图,在该视图中有一些我以编程方式创建的UIButton(参见下面的示例,只有一个按钮).

现在,当我按下按钮时,我想调用我的方法"doSomething".我目前这样做的方式是这样的:
[myButton addTarget:nil
action:@selector(doSomething:)
forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
由于我的目标是nil,它会响应响应者链,直到找到一个名为"doSomething"的方法.它有效,但它感觉不对.
我已经开始考虑使用@protocol,但并没有真正了解它.我一直在看一些教程,但对我来说还不够清楚.我已经使用了类似于表视图控制器的协议,但定义一个对我来说是新的.
是否有可能为这个特定案例得到一个例子?
谢谢!
我已使用以下代码在我的应用中成功安排了本地通知:
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil) {
UILocalNotification *notification = [[cls alloc] init];
notification.fireDate = self.alarmNotificationDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = @"Alarm is due";
notification.alertAction = @"Show Alarm";
notification.soundName = @"alarm.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];
Run Code Online (Sandbox Code Playgroud)
其中self.alarmNotificationDate是另一种方法中的NSDate赋值.
一切都在模拟器中工作正常,但是当我在运行iOS4的可信赖的旧iPhone 3G上测试时,我收到通知,但只有默认声音.
我试过不同的声音文件,但没有成功.
任何线索为什么它可能像这样以及如何纠正它?
谢谢
我在视图中使用两个UIPageControl来反映章节和页面.所以我定义了一个名为chapterCount的UIPageControl和一个名为pageCount的文件,以显示用户所在的位置.我使用滑动处理页面的翻转,而不是使用UIPageControls(因此没有为它们定义方法).
我在用户使用以下代码更改页面时更改其值:
chapterCount.numberOfPages = chapters;
chapterCount.currentPage = chapterNumber;
pageCount.numberOfPages = pages;
pageCount.currentPage = pageNumber;
[chapterCount updateCurrentPageDisplay];
[pageCount updateCurrentPageDisplay];
Run Code Online (Sandbox Code Playgroud)
章节,chapterNumber,pages和pageNumber都是整数.
我第一次浏览页面时通常可以正常工作,但是当我转到上一章或开始新的章节时,第一个(或最后一个取决于方向)点不显示.请看图片(上半部分缺少一个点,下部一个OK).
alt text http://img80.imageshack.us/img80/3339/pagecontrol.png
它更新控件的代码相同,有时我得到一个点,有时不是.当我使用NSLOG检查变量时,它们是正确的(例如,对于上面两张图片都是相同的).
我检查了Apple的文档并尝试了他们的例子,但没有运气.
我应该在哪里开始排除故障 我疯了!
iphone ×5
objective-c ×2
xcode ×2
audio ×1
default ×1
inheritance ×1
init ×1
ios ×1
localization ×1
nsdate ×1
nstimer ×1
protocols ×1
uibutton ×1