如題,繼續小白學Ob-c,先謝各位大大!
想要把textfield中輸入的變量存儲在table中,附上代碼:
在HWviewcontroller中的:
'''
@interface HWViewController ()
@property (nonatomic, strong) HWTableViewController *myTableViewController;
@property (strong, nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;
@property (strong, nonatomic) IBOutlet UITextField *main;
@property (strong, nonatomic) IBOutlet UITextField *detail;
@property (strong, nonatomic) IBOutlet UIButton *addButton;
@property (strong, nonatomic) IBOutlet UIButton *showButton;
@property (strong, nonatomic) NSMutableDictionary *myItem;
@property (strong, nonatomic) NSString *combinedWord;
@end
@implementation HWViewController
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"return button pressed");
[self.main resignFirstResponder];
[self.detail resignFirstResponder];
return YES;
}
-(IBAction)addPressed:(id)sender {
NSString *item=self.main.text;
NSString *detail=self.detail.text;
NSString *combine=[NSString stringWithFormat:@"%@%@",item,detail];
self.combinedWord=combine;
NSMutableArray *myWordArray = [[NSMutableArray alloc] init];
[myWordArray addObject:item];
[myWordArray addObject:detail];
[self.myItem setObject:myWordArray forKey:combine];
}
-(IBAction)showPressed:(id)sender {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.myTableViewController];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Return"
style:UIBarButtonItemStyleDone target:self
action:@selector(dismissModalViewControllerAnimated:)];
[self.myTableViewController.navigationItem setLeftBarButtonItem:doneButton];
[self presentViewController:navController animated:YES completion:nil];
}
'''
然後tableviewcontroller的:
'''
@interface HWTableViewController ()
@end
@implementation HWTableViewController
(id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
(void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// 取消注釋以下行以在該視圖控製器的導航欄中顯示編輯按鈕。
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// 返回節數。
返回 2;
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 返回該部分中的行數。
返回 2;
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
int r = indexPath.row;
int s = indexPath.section;
//這裏有問題的敵方,明顯不見
cell.textLabel.text = 這裏想是 self.main
cell.detailTextLabel.text = 這裏就像self.detail
if (r%2 == 0) {
cell.backgroundColor = [UIColor redColor];
}
其他{
cell.backgroundColor = [UIColor 白色顏色];
}
返回單元格;
}
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int r = indexPath.row;
int s = indexPath.section;
NSString *string = [NSString stringWithFormat:@"第 %d 節,第 %d 行", s, r];
NSLog(@"%@", 字符串);
[tableView deselectRowAtIndexPath:indexPath 動畫:YES];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"選擇"消息:@"您已選擇一行!" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:@"那不行", nil];
[alertView 顯示];
'''
cell.textLabel.text = 这里想是self.main
cell.detailTextLabel.text = 这里像是self.detail
這樣顯然是不行的,因為你的tableview裡面根本就沒有
self.main
,self.detail
這2個值,這個其實想解決有好幾種方式,其中最簡單的做法是在tableview中聲明2個屬性,跳轉控制器之前把tableview的2個屬性賦值.(
记住,tableview有代理和数据源
)退出鍵盤有更方便的方式
[self.view endEditing:YES];