2016-11-24 120 views
1

我在我的項目中創建了兩個視圖。我希望能夠點擊我的主視圖上的按鈕,另一個視圖(ChooseCar)會彈出,允許用戶選擇一些東西,然後它將重新打開輸入信息的舊視圖(ViewController)。我已經完成了它的代碼,但由於某種原因,當我點擊按鈕時,屏幕變黑並且什麼也沒有發生,我很確定這是非常簡單的事情,我只是無法理解它。iOS presentViewController代表返回黑色屏幕

我會附上下面的意見代碼,謝謝。

ViewController.h

// 
// ViewController.h 
// 
// Created by Curtis Boylan on 24/11/2016. 
// Copyright © 2016. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import "ChooseCar.h" 

@interface ViewController : UIViewController <ChooseCarDelegate> 
- (IBAction)chooselocation; 
@property (strong, nonatomic) IBOutlet UILabel *wherelocation; 

@end 

ViewController.m

// 
// ViewController.m 
// 
// Created by Curtis Boylan on 24/11/2016. 
// Copyright © 2016. All rights reserved. 
// 

#import "ViewController.h" 

@interface ViewController() 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
} 
- (IBAction)chooselocation { 
    ChooseCar *acController = [[ChooseCar alloc] init]; 
    // acController.delegate = self; 
    [self presentViewController:acController animated:YES completion:nil];} 

- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item 
{ 
    NSLog(@"This was returned from ChooseCar %@",item); 
} 

@end 

ChooseCar.h

// 
// ChooseCar.h 
// 
// Created by Curtis Boylan on 24/11/2016. 
// Copyright © 2016. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
@class ChooseCar; 

@protocol ChooseCarDelegate <NSObject> 
- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item; 
@end 

@interface ChooseCar : UIViewController 

@end 

ChooseCar.m

// 
// ChooseCar.m 
// 
// Created by Curtis Boylan on 24/11/2016. 
// Copyright © 2016. All rights reserved. 
// 

#import "ChooseCar.h" 

@interface ChooseCar() 
@property (nonatomic, weak) id <ChooseCarDelegate> delegate; 
@end 

@implementation ChooseCar 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    NSString *itemToPassBack = @"Pass this value back to ViewControllerA"; 
    [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack]; 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

@end 
+0

我沒有看到你解僱呈現VC。你不應該在委託方法中做到這一點嗎? – danh

+0

剛剛嘗試過,仍然給我一個黑色的屏幕 –

+0

你有使用storyboard或xib來創建'ChooseCar' ViewController嗎? – Mahesh

回答

2

它很容易做到:

1)我相信你的ChooseCar VC在storyboard創建。 如果是,你應該設置Storyboard ID這樣的:

set storyboard id

2)在你的ViewController.m,更新chooselocation方法是:

- (IBAction)chooselocation { 
//ChooseCar *acController = [[ChooseCar alloc] init]; 

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 


ChooseCar *acController = [sb instantiateViewControllerWithIdentifier:@"ChooseCar"]; 

[self presentViewController:acController animated:YES completion:nil]; 

} 

編輯

如果您想通過代表傳遞價值:

在我給的基礎上。

1)削減該代碼@property (nonatomic, weak) id <ChooseCarDelegate> delegate;ChooseCar.mChooseCar.h,確保ChooseCar.h這樣的:

#import <UIKit/UIKit.h> 

@class ChooseCar; 

@protocol ChooseCarDelegate <NSObject> 


- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item; 

@end 


@interface ChooseCar : UIViewController 

@property (nonatomic, weak) id <ChooseCarDelegate> delegate; 

@end 

2)在ViewController.m,你應該遵守protocal,並設置caController的代表。

#import "ChooseCar.h" 
#import "ViewController.h" 

@interface ViewController() <ChooseCarDelegate> 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
} 
- (IBAction)chooselocation { 
    //ChooseCar *acController = [[ChooseCar alloc] init]; 

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 


    ChooseCar *acController = [sb instantiateViewControllerWithIdentifier:@"ChooseCar"]; 

    acController.delegate = self; 

    [self presentViewController:acController animated:YES completion:nil]; 

} 

- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item 
{ 
    NSLog(@"This was returned from ChooseCar %@",item); 
} 

@end 

3)如果你想傳遞的價值,你應該把這個代碼到你的動作:

NSString *itemToPassBack = @"Pass this value back to ViewControllerA"; 
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack]; 
+0

這工作,並打開choosecar視圖,但不帶我回視圖控制器,並傳遞值 –

+0

@Curtis Boylan我編輯我的答案,你看。 – aircraft

+0

作品完美,謝謝! –

1

改爲如下代碼:

視圖控制器。^ h

#import <UIKit/UIKit.h> 
#import "ChooseCar.h" 

@interface ViewController : UIViewController <ChooseCarDelegate> 
- (IBAction)chooselocation; 
@property (strong, nonatomic) IBOutlet UILabel *wherelocation; 

@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 
{ 
    ChooseCar *acController; 
} 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
} 
- (IBAction)chooselocation { 

    acController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChooseCar"]; 
    acController.delegate = self; 
    [self presentViewController:acController animated:YES completion:nil]; 

} 

- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item 
{ 
    [acController dismissViewControllerAnimated:true completion:nil]; 
    NSLog(@"This was returned from ChooseCar %@",item); 
} 

@end 

而且設置故事板故事板ID。更多詳情請參閱隨附的屏幕截圖:

enter image description here

ChooseCar.h

#import <UIKit/UIKit.h> 
@class ChooseCar; 

@protocol ChooseCarDelegate <NSObject> 
- (void)addItemViewController:(ChooseCar *)controller didFinishEnteringItem:(NSString *)item; 
@end 

@interface ChooseCar : UIViewController 
@property (nonatomic, weak) id <ChooseCarDelegate> delegate; 
@end 

ChooseCar.m

#import "ChooseCar.h" 

@interface ChooseCar() 
@end 

@implementation ChooseCar 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

// NSString *itemToPassBack = @"Pass this value back to ViewControllerA"; 
// [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack]; 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (IBAction)backButtonClicked:(id)sender { 
    NSString *itemToPassBack = @"Pass this value back to ViewControllerA"; 
    [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack]; 
} 

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
// Get the new view controller using [segue destinationViewController]. 
// Pass the selected object to the new view controller. 
} 
*/ 

@end 
+0

是的,它可以很好地工作,但它會打開ChooseCar視圖並且不會返回到ViewController。它也不會從ChooseCar中返回項目。任何解決方案,使其工作? –

+0

你什麼時候想回來?點擊按鈕? – User511

+0

是的,在按鈕上單擊我希望它將您帶回另一個視圖並傳回項目值。 –