iOS presentViewController 委托(delegate)返回黑屏

标签 ios objective-c delegates presentviewcontroller

我在我的项目中创建了两个 View 。我希望能够在我的主视图上单击一个按钮,然后另一个 View (ChooseCar)会弹出,允许用户选择一些东西,然后它将重新打开旧 View (ViewController)并输入信息。我已经完成了它的代码,但是由于某种原因,当我单击按钮时,屏幕变黑了,什么也没有发生,我很确定这是非常简单的事情,我只是无法理解它。

我将附上以下 View 的代码,谢谢。

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

选择汽车.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

选车.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像这样:

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];

}

编辑

如果你想通过委托(delegate)传递一个值:

在我给予的基础上。

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的委托(delegate)。
#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];

关于iOS presentViewController 委托(delegate)返回黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40794630/

相关文章:

ios - 如何检查 iPhone 和 Apple Watch 是否连接

iOS 应用程序使用临时分发配置文件崩溃

ios - 发生 JSON 解析错误,详细信息如下 :

swift - 将数据从容器 VC 传递到 XLPagerTabStrip 中的子 VC

c# - 如何处理表达式树中的闭包和静态方法调用?

powershell - 如何使用 System.RuntimeCaching.MemoryCache 正确实现 PowerShell 回调

ios - iOS 应用程序中 UIToolbar 的奇怪行为

ios - Swift 在将 NSString 转换为 NSDate 时显示与 Objective-C 不同的结果?

iphone - 创建许多对象时 FPS(每秒帧数)下降

iPhone GPS - 未收到有效的位置更新