我在我的項目中創建了兩個視圖。我希望能夠點擊我的主視圖上的按鈕,另一個視圖(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
我沒有看到你解僱呈現VC。你不應該在委託方法中做到這一點嗎? – danh
剛剛嘗試過,仍然給我一個黑色的屏幕 –
你有使用storyboard或xib來創建'ChooseCar' ViewController嗎? – Mahesh