关于一个简单的学生列表app的问题

lawlite9 2014-07-29 05:39:06
新手,最近正好在学UITableVIewController方面的知识,然后就想试着自己写一个简单的小app,效果图如下:
分别是StudentListTableViewController和StudentDetailsViewController,预期是想选择一个cell,然后StudentDetailsVIewController显示具体的消息。但是点进去Detail那个界面并没有具体的数据。
我在StudentDetailsViewController里声明了一个currentStudent属性

然后这是StudentListTableViewController中的didSelectedRowAtIndexPath方法:

在didSelectedRowAtIndexPath方法中和StudentDetailsVIewController的viewDidLoad方法中都用NSLog(@"%@",currentStudent)来调试,结果当我选择了一个cell后终端显示了具体值,但是页面切换到StudentDetailsVIewController时,终端显示的是null,照理说不应该为null,而且书上的例子这么做是可以如期运行的。纠结了一个下午了,实在想不透,便来论坛寻求帮组,望指点迷津。
...全文
154 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
KralLee 2014-07-30
  • 打赏
  • 举报
回复
还有你的代理应该是assign的
@property (nonatomic, assign) id <StudentDetailsViewControllerDelegate> delegate;
KralLee 2014-07-30
  • 打赏
  • 举报
回复
如下图,把strong改成retain
Bannings 2014-07-30
  • 打赏
  • 举报
回复
你是用的Storyboard吗?我在你的didSelect方法里面没看到pushViewController,如果你是用的Storyboard,要在prepareForSegue里面处理,就像这样:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)cell{
    ViewController *destVC = (ViewController *)segue.destinationViewController;
    destVC.currentStudent = chosenStudent;
}
virtualxmars 2014-07-30
  • 打赏
  • 举报
回复
应该是你没有传值给currentStudent属性,需要把StudentListTableViewController相关代码贴出来
Bannings 2014-07-30
  • 打赏
  • 举报
回复
引用 9 楼 u012340887 的回复:
[quote=引用 4 楼 zhangao0086 的回复:] 你是用的Storyboard吗?我在你的didSelect方法里面没看到pushViewController,如果你是用的Storyboard,要在prepareForSegue里面处理,就像这样:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)cell{
    ViewController *destVC = (ViewController *)segue.destinationViewController;
    destVC.currentStudent = chosenStudent;
}
我用的是storyboard用你的方法我终于成功解决了问题,但是我之前照着书上做书上的例子,并没有用prepareForSegue,而是像我之前的那样,也成功了[/quote] 解决了就好
lawlite9 2014-07-30
  • 打赏
  • 举报
回复
引用 4 楼 zhangao0086 的回复:
你是用的Storyboard吗?我在你的didSelect方法里面没看到pushViewController,如果你是用的Storyboard,要在prepareForSegue里面处理,就像这样:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)cell{
    ViewController *destVC = (ViewController *)segue.destinationViewController;
    destVC.currentStudent = chosenStudent;
}
我用的是storyboard用你的方法我终于成功解决了问题,但是我之前照着书上做书上的例子,并没有用prepareForSegue,而是像我之前的那样,也成功了
Bannings 2014-07-30
  • 打赏
  • 举报
回复
引用 7 楼 u012340887 的回复:
[quote=引用 5 楼 aas319 的回复:] 如下图,把strong改成retain
把哪个属性的strong改成retain?[/quote] ARC环境下,strong = retain
lawlite9 2014-07-30
  • 打赏
  • 举报
回复
引用 5 楼 aas319 的回复:
如下图,把strong改成retain
把哪个属性的strong改成retain?
lawlite9 2014-07-29
  • 打赏
  • 举报
回复
这是.h文件里的:
#import <UIKit/UIKit.h>
#import "Student.h"

@class StudentDetailsViewController;

@protocol StudentDetailsViewControllerDelegate <NSObject>

- (void)studentDetailsViewControllerDidFinish:(StudentDetailsViewController *)sender;

@end

@interface StudentDetailsViewController : UIViewController<UITextFieldDelegate>

@property (strong, nonatomic) Student *currentStudent;

@property (weak, nonatomic) IBOutlet UITextField *classNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *numberTextField;

@property (weak, nonatomic) id<StudentDetailsViewControllerDelegate>delegate;

- (IBAction)revert:(id)sender;
@end
这是.m文件里的:
#import "StudentDetailsViewController.h"

@interface StudentDetailsViewController ()

@end

@implementation StudentDetailsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.    
}

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


- (IBAction)revert:(id)sender {
    [self populateViewWithStudent:self.currentStudent];
}


// Populate the data
- (void)populateViewWithStudent:(Student *)student
{
    self.currentStudent = student;
    
    self.classNameTextField.text = student.className;
    self.nameTextField.text = student.name;
    self.numberTextField.text = student.number;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self populateViewWithStudent:self.currentStudent];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.view.window endEditing:YES];
    
    self.currentStudent.name = self.nameTextField.text;
    self.currentStudent.className = self.classNameTextField.text;
    self.currentStudent.number = self.numberTextField.text;
    [self.delegate studentDetailsViewControllerDidFinish:self];
}
Bannings 2014-07-29
  • 打赏
  • 举报
回复
首先,你的delegate要用weak或者assign,用strong会导致循环引用。currentStudent的问题,最好把StudentDetailsVIewController这个类贴出来看一下。

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧