[英](iOS) crash when view controller property is set
我在將數據傳遞到新的視圖控制器時遇到問題。
我有兩個VC。 在“ FirstVC”中,有一個帶有動態原型單元的表格視圖。 通用單元包含一個標簽和一個按鈕,並將其分配給自己的類“ GenericCell”。 我想在按下按鈕時將單元格標簽的文本傳遞給SecondVC,但該應用程序因錯誤而崩潰:
接口生成器文件中的未知類_TtC13testTableView37SecondVC。
UIViewController setMyString:]:無法識別的選擇器已發送到實例0x7fa05e418110
由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'-[UIViewController setMyString:]:無法識別的選擇器已發送到實例0x7fc8add05710
我試過了:
在getText方法之外使用segue(帶有prepareForSegue方法)
1a。 從getText方法的外部推送VC
實現並調用myString的set / get方法
將屬性添加到myString屬性
但是應用程序崩潰了。
通用細胞
#import <UIKit/UIKit.h>
@protocol MyCellDelegate
-(void)getText:(NSString *)text;
@end
@interface GenericCell: UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel * label;
@property (assign, nonatomic) id <MyCellDelegate> delegate;
@end
通用細胞
#import "GenericCell.h"
@interface GenericCell()
@end
@implementation GenericCell
- (IBAction)buttonPressed {
if (self.delegate) {
[self.delegate getText:self.label.text];
}
}
@end
FirstVC.m
#import "FirstVC.h"
#import "GenericCell.h"
#import "SecondVC.h"
@interface FirstVC() <UITableViewDataSource,UITableViewDelegate, MyCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property NSString * idToPass;
@end
@implementation FirstVC
NSMutableArray<NSString *> *array;
- (void)viewDidLoad {
array = [[NSMutableArray alloc]initWithObjects:@"0x22", @"0x11", @"0x24", nil];
[super viewDidLoad];
self.tableView.dataSource=self;
self.tableView.delegate=self;
self.tableView.rowHeight=100;
}
-(void)getText:(NSString *)text {
self.idToPass = text;
SecondVC * secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondVC"];
secondVC.myString = self.idToPass;
[[self navigationController] pushViewController:secondVC animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return array.count;
}
- (GenericCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
GenericCell * cell = [[self tableView]dequeueReusableCellWithIdentifier:@"GenericCell" forIndexPath:indexPath];
cell.delegate = self;
cell.label.text = [array objectAtIndex:indexPath.row];
return cell;
}
@end
SecondVC.h
#import <UIKit/UIKit.h>
@interface SecondVC : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *label;
@property NSString * myString;
@end
SecondVC.m
#import "SecondVC.h"
@interface SecondVC ()
@end
@implementation SecondVC
- (void)viewDidLoad {
self.label.text = self.myString;
[super viewDidLoad];
}
@end
使用委托正確設置了“ idToPass”。
在getText方法中,將文本分配給myProperty而不是myString ...
它應該是:
secondVC.myString = self.idToPass;
我的工作:在buttonPressed方法中更新if語句,如下所示
if ([self.delegate respondsToSelector:@selector(getText:)]){
[self.delegate getText:self.label.text];
}
提供SecondVC數據的另一種選擇是使用
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
SecondVC * vc = segue.destinationViewController;
if ([segue.identifier isEqualToString: @"yourSegue"]) {
vc.myString = self.idToPass;
}
}
我建議的一個提示是(使用簡單的if語句)檢查SecondVC中的對象是否為nil或!nil
我希望這個幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.