我在项目中创建了两个视图。我想能够点击主视图上的按钮,然后会弹出另一个视图(ChooseCar),允许用户选择一些东西,然后它将使用输入的信息重新打开旧视图(ViewController)。我已经为它完成了代码,但由于某种原因,当我点击按钮时,屏幕变黑,没有任何事情发生,我很确定这是非常简单的事情,我无法理解我的头脑在它周围。
我将附上以下观点的代码,谢谢。
的 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 :(得分:2)
很容易做到:
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];
答案 1 :(得分:1)
使用以下内容替换您的代码:
<强> ViewController.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