objective-c - 如何从tableViewcontroller中调用变量?
阿神
阿神 2017-04-22 09:00:41
0
1
415

如题,继续小白学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;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

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

pragma mark - Table view data source

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {

    // Return the number of sections.
    return 2;
    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {

    // Return the number of rows in the section.
    return 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];
    }
    else{
    cell.backgroundColor = [UIColor whiteColor];
    }

    return cell;
    }

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    int r = indexPath.row;
    int s = indexPath.section;
    NSString *string = [NSString stringWithFormat:@"Section %d, Row %d", s, r];
    NSLog(@"%@", string);

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Selection" message:@"You have selected a row!" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:@"Thatz not okay", nil];

    [alertView show];
    '''

阿神
阿神

闭关修行中......

모든 응답(1)
刘奇

cell.textLabel.text = 这里想是self.main
cell.detailTextLabel.text = 这里像是self.detail
테이블뷰에 ​​​​self.main, self.detail
값이 없기 때문에 이는 분명히 불가능합니다. 실제로 이 문제를 해결하는 방법에는 여러 가지가 있습니다. 가장 간단한 방법은 컨트롤러로 점프하기 전에 테이블뷰에 두 개의 속성을 선언하고 값을 할당하는 것입니다. 记住,tableview有代理和数据源키보드를 종료하는 더 편리한 방법이 있습니다


최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿