iOS presentViewController代表返回黑色屏幕
我在我的项目中创建了两个视图。我希望能够点击我的主视图上的按钮,另一个视图(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
它很容易做到:
1)我相信你的ChooseCar
VC在storyboard
创建。 如果是,你应该设置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.m
到ChooseCar.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];
改为如下代码:
视图控制器。^ 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。更多详情请参阅随附的屏幕截图:
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
是的,它可以很好地工作,但它会打开ChooseCar视图并且不会返回到ViewController。它也不会从ChooseCar中返回项目。任何解决方案,使其工作? –
你什么时候想回来?点击按钮? – User511
是的,在按钮上单击我希望它将您带回另一个视图并传回项目值。 –
我没有看到你解雇呈现VC。你不应该在委托方法中做到这一点吗? – danh
刚刚尝试过,仍然给我一个黑色的屏幕 –
你有使用storyboard或xib来创建'ChooseCar' ViewController吗? – Mahesh