objective-c - 如何從tableViewcontroller呼叫變數?
阿神
阿神 2017-04-22 09:00:41
0
1
461

如題,繼續小白學Ob-c,先謝各位大大!
想要把textfield中輸入的變量存儲在table中,附上代碼:
在HWviewcontroller中的:
'''

import "HWViewController.h"

import "HWTableViewController.h"

@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

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.main.delegate = self;
    self.detail.delegate = self;
    self.myItem = [[NSMutableDictionary alloc] init];
    self.myTableViewController = [[HWTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    self.myTableViewController.view.frame = self.view.bounds;

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

}

  • (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

'''

然後tableviewcontroller的:
'''

import "HWTableViewController.h"

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

}

  • (void)didReceiveMemoryWarning
    {
    [超級didReceiveMemoryWarning];
    // 處理所有可以重新創建的資源。
    }

pragma mark - 表視圖數據源

  • (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 顯示];
    '''

阿神
阿神

闭关修行中......

全部回覆(1)
刘奇

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板