Iphone UIApplicationWillEnterForegroundNotification未调用
嗨,我是Iphone开发的新手。我有一个问题: 我在Appdeligate类中使用UIApplicationDeligate,在这个类中我使用两个方法 应用程序标识符背景和应用程序接口前景。然后这些方法工作得很好,当我的应用程序进入后台或前台时,流程进入这些方法 但在另一个UiViewController类中,我附加了UIApplicationIdentiterForeGroundNotification和UIApplicationIdentiterBackgroundNotification的侦听器 现在,当我的应用程序进入后台时,流进入后台方法,但当我的应用程序进入前台时,应用程序流永远不会进入前台方法 我在AppDelegate类中使用的代码:Iphone UIApplicationWillEnterForegroundNotification未调用,iphone,uiapplicationdelegate,Iphone,Uiapplicationdelegate,嗨,我是Iphone开发的新手。我有一个问题: 我在Appdeligate类中使用UIApplicationDeligate,在这个类中我使用两个方法 应用程序标识符背景和应用程序接口前景。然后这些方法工作得很好,当我的应用程序进入后台或前台时,流程进入这些方法 但在另一个UiViewController类中,我附加了UIApplicationIdentiterForeGroundNotification和UIApplicationIdentiterBackgroundNotification的
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:timertimetobefired forKey:@"TimerTimeToFired"];
[defaults setValue:timerrecipename forKey:@"TimerRecipeName"];
[defaults setObject:storingbuttonstate forKey:@"TimerButtonState"];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
timertimetobefired = [defaults objectForKey:@"TimerTimeToFired"];
timerrecipename = [defaults objectForKey:@"TimerRecipeName"];
storingbuttonstate = [defaults objectForKey:@"TimerButtonState"];
}
以及我在UiViewController类中使用的代码:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(goBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:@"forground"
selector:@selector(doMyLayoutStuff:)
name: UIApplicationWillEnterForegroundNotification
object:nil];
- (void) goBackground
{
if(timer != nil && [timer isValid])
{
[timer invalidate], timer = nil;
}
}
谁能告诉我这个代码有什么问题吗。提前谢谢
doMylayoutStuff中的代码:
- (void)doMyLayoutStuff:(id)sender
{
fortimer=0;
appdelegate.imgRecipePhoto=nil;
recipeMakingTime=0;
arrForStepsReminder=[[NSMutableArray alloc]init];
[arrForStepsReminder removeAllObjects];
arrForSteps5=[[NSMutableArray alloc]init];
[arrForSteps5 removeAllObjects];
arrForButtonSelection=[[NSMutableArray alloc]init];
[arrForButtonSelection removeAllObjects];
[arrForSteps5 addObjectsFromArray:appdelegate.arrStepsandTime];
arrayforshowingdata = [[NSMutableArray alloc]initWithCapacity:arrForSteps5.count];
for (int i=0; i<[arrForSteps5 count]; i++)
{
//nitin tyagi. 6/08/2013.
// NSString *str12=[[arrForSteps5 objectAtIndex:i]objectForKey:@"RecipestepTime"];
// if (str12.length==0)
// {
// [arrForSteps5 removeObjectAtIndex:i];
// }
// else
// {
// }
NSString *timer1 = [[arrForSteps5 objectAtIndex:i]objectForKey:@"RecipeTimer1"];
NSString *timer2 = [[arrForSteps5 objectAtIndex:i]objectForKey:@"RecipeTimer2"];
NSString *timer3 = [[arrForSteps5 objectAtIndex:i]objectForKey:@"RecipeTimer3"];
NSString *timer4 = [[arrForSteps5 objectAtIndex:i]objectForKey:@"RecipeTimer4"];
timer1 = [timer1 stringByReplacingOccurrencesOfString:@" " withString:@""];
timer2 = [timer2 stringByReplacingOccurrencesOfString:@" " withString:@""];
timer3 = [timer3 stringByReplacingOccurrencesOfString:@" " withString:@""];
timer4 = [timer4 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableArray *finaltimerarray = [[NSMutableArray alloc]init];
int totalminute = 0;
int hours = 0;
if(([timer1 hasSuffix:@"mins"] || [timer1 hasSuffix:@"minute"] || [timer1 hasSuffix:@"min"]) || ([timer1 isEqualToString:@"0"]))
{
if(![timer1 isEqualToString:@"0"]){
NSRange range = [timer1 rangeOfString:@"min"];
NSString *minutes = [timer1 substringToIndex:range.location];
totalminute = totalminute + [minutes intValue];
NSNumber* xWrapped = [NSNumber numberWithInt:[minutes intValue]];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(@"totalminute minute is = %@",totalminute);
}
else if(([timer1 hasSuffix:@"hours"] || [timer1 hasSuffix:@"hrs"] || [timer1 hasSuffix:@"hour"] || [timer1 hasSuffix:@"hr"]) )
{
if(![timer1 isEqualToString:@"0"]){
NSRange range = [timer1 rangeOfString:@"h"];
NSString *hours = [timer1 substringToIndex:range.location];
int minute = [hours intValue] * 60;
totalminute = totalminute + minute;
NSNumber* xWrapped = [NSNumber numberWithInt:minute];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(@"timer1 minute is = %@",minutes);
}
if(([timer2 hasSuffix:@"mins"] || [timer2 hasSuffix:@"minute"] || [timer2 hasSuffix:@"min"]) || ([timer2 isEqualToString:@"0"]))
{
if(![timer2 isEqualToString:@"0"]){
NSRange range = [timer2 rangeOfString:@"min"];
NSString *minutes = [timer2 substringToIndex:range.location];
totalminute = totalminute + [minutes intValue];
NSNumber* xWrapped = [NSNumber numberWithInt:[minutes intValue]];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(@"timer2 minute is = %@",minutes);
}
else if(([timer2 hasSuffix:@"hours"] || [timer2 hasSuffix:@"hrs"] || [timer2 hasSuffix:@"hour"] || [timer2 hasSuffix:@"hr"]) )
{
if(![timer2 isEqualToString:@"0"])
{
NSRange range = [timer2 rangeOfString:@"h"];
NSString *hours = [timer2 substringToIndex:range.location];
int minute = [hours intValue] * 60;
totalminute = totalminute + minute;
NSNumber* xWrapped = [NSNumber numberWithInt:minute];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(@"timer1 minute is = %@",minutes);
}
if(([timer3 hasSuffix:@"mins"] || [timer3 hasSuffix:@"minute"] || [timer3 hasSuffix:@"min"]) || ([timer3 isEqualToString:@"0"]))
{
if( ![timer3 isEqualToString:@"0"])
{
NSRange range = [timer3 rangeOfString:@"min"];
NSString *minutes = [timer3 substringToIndex:range.location];
totalminute = totalminute + [minutes intValue];
NSNumber* xWrapped = [NSNumber numberWithInt:[minutes intValue]];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(@"timer3 minute is = %@",minutes);
}
else if(([timer3 hasSuffix:@"hours"] || [timer3 hasSuffix:@"hrs"] || [timer3 hasSuffix:@"hour"] || [timer3 hasSuffix:@"hr"]) )
{
if(![timer3 isEqualToString:@"0"]){
NSRange range = [timer3 rangeOfString:@"h"];
NSString *hours = [timer3 substringToIndex:range.location];
int minute = [hours intValue] * 60;
totalminute = totalminute + minute;
NSNumber* xWrapped = [NSNumber numberWithInt:minute];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(@"timer1 minute is = %@",minutes);
}
if(([timer4 hasSuffix:@"mins"] || [timer4 hasSuffix:@"minute"] || [timer4 hasSuffix:@"min"]) || ([timer4 isEqualToString:@"0"]))
{
if(![timer4 isEqualToString:@"0"])
{
NSRange range = [timer4 rangeOfString:@"min"];
NSString *minutes = [timer4 substringToIndex:range.location];
totalminute = totalminute + [minutes intValue];
NSNumber* xWrapped = [NSNumber numberWithInt:[minutes intValue]];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(@"timer4 minute is = %@",minutes);
}
else if(([timer4 hasSuffix:@"hours"] || [timer4 hasSuffix:@"hrs"] || [timer4 hasSuffix:@"hour"] || [timer4 hasSuffix:@"hr"]) )
{
if(![timer4 isEqualToString:@"0"]){
NSRange range = [timer4 rangeOfString:@"h"];
NSString *hours = [timer4 substringToIndex:range.location];
int minute = [hours intValue] * 60;
totalminute = totalminute + minute;
NSNumber* xWrapped = [NSNumber numberWithInt:minute];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(@"timer1 minute is = %@",minutes);
}
[arrayforshowingdata addObject:finaltimerarray];
[finaltimerarray release];
}
for (int i=0; i<[arrayforshowingdata count]; i++)
{
NSMutableArray *arraydata = [arrayforshowingdata objectAtIndex:i];
for (int j=0; j<[arraydata count]; j++)
{
[arrForStepsReminder addObject:@"0"];
}
[arrForButtonSelection addObject:@"M"];
}
lblDisplayTimer.text=@"00:00:00";
lblRecipeName.text=appdelegate.strRecipeName;
currentdate = [NSDate date];
NSDate *timetobefired = appdelegate.timertimetobefired;
timerrecipe = appdelegate.timerrecipename;
if(timerrecipe != nil && ([timerrecipe isEqualToString:appdelegate.strRecipeName]) && timetobefired != nil)
{
NSTimeInterval secondsBetween = [timetobefired timeIntervalSinceDate:currentdate];
NSInteger time = round(secondsBetween);
int second = (int)time;
if(second > 0)
{
secondsLeft = second;
arrForButtonSelection = appdelegate.storingbuttonstate;
[btnOnOff setOn:YES];
if(![timer isValid])
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES];
}
}
else
{
secondsLeft = 0;
}
}
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(goBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(doMyLayoutStuff:)
name: UIApplicationWillEnterForegroundNotification
object:nil];
}
-(void)域:(id)发送方
{
fortimer=0;
appdelegate.imgRecipePhoto=nil;
recipeMakingTime=0;
arrForStepsReminder=[[NSMutableArray alloc]init];
[arrForStepsReminder removeAllObjects];
arrForSteps5=[[NSMutableArray alloc]init];
[arrForSteps5移除所有对象];
arrForButtonSelection=[[NSMutableArray alloc]init];
[arrForButtonSelection removeAllObjects];
[arrForSteps5 addObjectsFromArray:appdelegate.arrforstepsandtime];
arrayforshowingdata=[[NSMutableArray alloc]initWithCapacity:arrForSteps5.count];
对于(int i=0;i在addObserver调用中将观察者更改为“self
”
i、 e
确保执行该行,并确保选择器完全相同。粘贴选择器的实现以获得进一步的答复..ravi现在工作正常..但现在您可以告诉我如何取消注册此通知。-removeObserver:name:object:使用该选项。但在询问之前先搜索。
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(doMyLayoutStuff:)
name: UIApplicationWillEnterForegroundNotification
object:nil];