重定向到FirstViewController

时间:2014-05-09 05:17:23

标签: ios objective-c uiviewcontroller viewcontroller

我正在使用不同的Viewcontroller为不同的屏幕设置应用程序。我有按钮点击,播放视频和加载HTML页面等。如果我在第二个视图控制器中打开应用程序和我的应用程序,然后我在1分钟内没有做任何事情,例如任何点击事件或应用程序中的任何触摸事件,自动我可以重定向到第一个视图控制器?有可能吗?

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.



    video=[[VideoViewController alloc]init];

     [self.navigationController.navigationBar setHidden:YES];

    bgImg=[[UIImageView alloc]init];

    [bgImg setFrame:CGRectMake(0,0,1024,768)];
    [bgImg setImage:[UIImage imageNamed:@"home_bg.png"]];
    [bgImg setUserInteractionEnabled:YES];

    [self.view addSubview:bgImg];


    video_play=[UIButton buttonWithType:UIButtonTypeCustom];
    // [info_btn setFrame:CGRectMake(240,10 ,13, 24)];

    [video_play setFrame:CGRectMake(118,300,362,202)];
    [video_play setImage:[UIImage imageNamed:@"video.png"] forState:UIControlStateNormal];
    [video_play addTarget:self action:@selector(watchvideo:) forControlEvents:UIControlEventTouchUpInside];
    //[rhymesbgImg addSubview:info_btn];
    [bgImg addSubview:video_play];


    watch_video=[UIButton buttonWithType:UIButtonTypeCustom];
    // [info_btn setFrame:CGRectMake(240,10 ,13, 24)];

    [watch_video setFrame:CGRectMake(180,600,232,60)];
    [watch_video setImage:[UIImage imageNamed:@"watch-video.png"] forState:UIControlStateNormal];
    [watch_video addTarget:self action:@selector(watchvideo:) forControlEvents:UIControlEventTouchUpInside];
    //[rhymesbgImg addSubview:info_btn];
    [bgImg addSubview:watch_video];

[self performSelector:@selector(goToRootViewController) withObject:nil afterDelay:20.0];


}

点击按钮的事件:

-(void)watchvideo:(id)sender{


  //   [self performSelector:@selector(goToRootViewController) withObject:nil afterDelay:20.0];

    [bgImg setHidden:YES];
    [watch_video setHidden:YES];
    [estimate_yoursaving setHidden:YES];
NSString *pathToBundle = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:pathToBundle];
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"html"];
    NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    //CGRect fullScreenRect=[[UIScreen mainScreen]applicationFrame];
    web=[[UIWebView alloc]initWithFrame:CGRectMake(172, 210, 680, 380)];
    [web loadHTMLString:htmlString baseURL:baseURL];


}

-(void)estimatesavings:(id)sender{

  //   [self performSelector:@selector(goToRootViewController) withObject:nil afterDelay:20.0];

    [web loadHTMLString:nil baseURL:nil];

    NSString *pathToBundle = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:pathToBundle];
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    //CGRect fullScreenRect=[[UIScreen mainScreen]applicationFrame];
    web_estimate=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024, 768)];
    [web_estimate loadHTMLString:htmlString baseURL:baseURL];

    web_estimate.delegate=self;
    [self.view addSubview:web_estimate];


}

触摸事件:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    isTouched = YES;

    NSLog(@"Touches happening");
}


- (void)goToRootViewController
{
    if (isTouched == NO)  {
      //  [self.navigationController popToRootViewControllerAnimated:YES];


        NSLog(@"Touches Not Happening");

        [bgImg setHidden:NO];
        [watch_video setHidden:NO];
        [estimate_yoursaving setHidden:NO];



       [web setHidden:YES];

        [web_estimate setHidden:YES];


    }

}

2 个答案:

答案 0 :(得分:1)

是的,确定。你可以弹出到根视图控制器

[self.navigationController popToRootViewControllerAnimated:YES];

修改

您可以通过此委托方法跟踪触摸事件

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    isTouched = YES;
}

在班级中添加方法

- (void)goToRootViewController
{
    if (isTouched == NO)  {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }

}

将此方法从 viewDidLoad 调用为:

[self performSelector:@selector(goToRootViewController) withObject:nil afterDelay:60.0];

答案 1 :(得分:0)

你的观点确实加载方法添加此代码

[self performSelector:@selector(pushOrPOP) withObject:nil afterDelay:1.0];

-(void)pushOrPOP
{
    [self.navigationController pushViewController:[[secViewController alloc]init] animated:YES];
     //[self.navigationController popToRootViewControllerAnimated:YES];
}