2016-02-12 63 views
0

我正在Swift項目中使用Swift以及Objective-C代碼。 我知道在Swift項目中使用Objective-C代碼我需要添加橋接頭。我已經包含並有效地使用了Objective-C代碼。要在Objective-C中使用Swift代碼,我需要在.m文件中包含#import "ProjectName-Swift.h"pushViewController從Objective-C代碼到Swift項目

現在發出我面對的是,我打電話LauchViewController.swift方法方法data.getLoadData()

func navigate() { 
    let data: Data = Data() 
    data.getLoadData() 
    self.navigationController!.pushViewController(DisplayCarViewController(), animated: true) 
} 

現在我想pushViewController另一個斯威夫特控制器DisplayCarViewController,它的tableView我必須填寫數據。 這裏是getLoadData的代碼()

- (void)getLoadData 
{ 
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    spinner.backgroundColor = [UIColor blackColor]; 
    [spinner setCenter:CGPointMake([[UIScreen mainScreen] bounds].size.width/2,[[UIScreen mainScreen] bounds].size.height/2)]; 
    [self.view addSubview:spinner]; 
    [spinner startAnimating]; 

    AFOAuth1OneLeggedClient *clnt = [[AFOAuth1OneLeggedClient alloc] initWithBaseURL:[NSURL URLWithString:path] key:cosumer_key secret:cosumer_secret]; 
    [clnt getPath:@"products" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     if ([responseObject isKindOfClass:[NSDictionary class]]) { 
      NSLog(@"responseObject: %@\n operation: %@", [responseObject valueForKey:@"products"], operation); 

      array = [[NSMutableArray alloc] init]; 
      [array addObjectsFromArray: [responseObject valueForKey:@"products"]]; 

      NSLog(@"Array: %@", array); 
      [spinner stopAnimating]; 
      DisplayCarViewController *controller = [[DisplayCarViewController alloc] init]; 
      [self.navigationController pushViewController:controller animated:true]; 
     } 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error data: %@\n Error operation: %@",error, operation); 
     [spinner stopAnimating]; 
    }]; 
} 

的問題是,應用程序停留在LauchViewController。數據成功獲取,但不要移動/推送到DisplayCarViewController。我究竟做錯了什麼?

+0

我不太確定你的問題 –

+0

我想推到另一個控制器在這裏DisplayCarViewController * controller = [[DisplayCarViewController alloc] init]; [self.navigationController pushViewController:controller animated:true];我無法這樣做。 –

+0

你是什麼意思?什麼是錯誤/結果? –

回答

0

您的getLoadData方法在Data類實現中。而這個類看起來不像UIViewController的子類。至少它不應該按照邏輯。我想在這裏最好的解決方法是將實現你這樣的getLoadData方法:

- (void)getLoadDataWithCompletion:(void(^)(NSArray *data, NSError *error))completion 
{ 
    AFOAuth1OneLeggedClient *clnt = [[AFOAuth1OneLeggedClient alloc] initWithBaseURL:[NSURL URLWithString:path] key:cosumer_key secret:cosumer_secret]; 
    [clnt getPath:@"products" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     if ([responseObject isKindOfClass:[NSDictionary class]]) { 
      NSLog(@"responseObject: %@\n operation: %@", [responseObject valueForKey:@"products"], operation); 

      array = [[NSMutableArray alloc] init]; 
      [array addObjectsFromArray: [responseObject valueForKey:@"products"]]; 

      NSLog(@"Array: %@", array); 
      completion ? completion(array, nil) : nil; 
     } 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error data: %@\n Error operation: %@",error, operation); 
     completion ? completion(nil, error) : nil; 
    }]; 
} 

並實現您navigate方法是這樣的。

func navigate() { 
    let data: Data = Data() 

    let spinner = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge) 
    spinner.backgroundColor = UIColor.blackColor() 
    let screenSize = UIScreen.mainScreen().bounds.size 
    spinner.center = CGPointMake(screenSize.width/2, screenSize.height/2) 
    view.addSubview(spinner) 
    spinner.startAnimating() 

    data.getLoadData() { 
     [unowned self] (data, error) in 
     spinner.stopAnimating() 
     if let data = data { 
      let carVC = DisplayCarViewController() 
      // init carVC with data somehow 
      self.navigationController?.pushViewController(carVC, animated: true) 
     } else { 
      // handle error somehow 
     } 
    } 

} 

我沒試過這樣的代碼可能有一些語法錯誤。

+0

哇非常感謝,我在這裏呆了很長時間,解決了我的問題。萬分感謝。 :) –

相關問題