I am getting values from @protocol, but can't use in UITableViewController?
In addAlarm Controller, I am declaring an NSString as below,
NSString *nameOfAlarm; // in .h
@property (nonatomic, retain) NSString *nameOfAlarm; //in .h
@synthesize nameOfAlarm; //in .m
and In ViewDidLoad, I am initializing it as following
nameOfAlarm = [[NSString alloc] initWithString:@"Alarm"];//In ViewDidLoad
then after I am doing something like below
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
// Configure the cell...
if (indexPath.section == 0) {
UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
cell.accessoryView = mySwitch;
[(UISwitch *)cell.accessoryView setOn:YES]; // Or NO, obviously!
[(UISwitch *)cell.accessoryView addTarget:self action:@selector(mySelector:)
开发者_如何学运维 forControlEvents:UIControlEventValueChanged];
NSString *cellValue = [NSString stringWithFormat:@"Enable Alarm"];
cell.textLabel.text = cellValue;
//return cell;
}
if (indexPath.section == 1) {
if (indexPath.row == 0) {
NSString *cellValue = [NSString stringWithFormat:@"Name "];
cell.textLabel.text = cellValue;
NSString *cellValue2 = [NSString stringWithFormat:@"%@",(NSString*)nameOfAlarm];
cell.detailTextLabel.text = cellValue2;
}
so I am doing reload table in ViewWillAppear as below
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"ViewWillAppear, The name is %@",nameOfAlarm);
[self.tableView reloadData];
}
I am writing delegate method which is being called by other controller as followed,
- (void) processName: (NSString *)n{
nameOfAlarm = (NSString *)n;
NSLog(@"Name is %@",nameOfAlarm);
}
Now when I click on 0 index of row, it will go to Name controller, simply, the .h of name controller is
#import <UIKit/UIKit.h>
@protocol ProcessNameDelegate <NSObject>
@required
- (void) processName: (NSString *)n;
@end
@interface Name : UITableViewController <UITextFieldDelegate>{
id <ProcessNameDelegate> delegate;
UITextField *name_textField;
}
@property (retain) id delegate;
@property (nonatomic, retain) UITextField *name_textField;
- (void)pressComplete;
@end
and when pop back to previous controller, then will call the following method is ViewWillDisappear,
- (void)pressComplete {
NSString *name = (NSString *)name_textField.text;
[[self delegate] processName:name];
}
This will set value to nameOfAlarm Everything is fine, but when I do reload then this is not showing in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
It is showing something like below if I do use nameOfAlarm in cell value or even just doing NSLog,
<_UIStretchableImage: 0x14db10> //the first time in nameOfAlarm,
the second time If I will go in Name controller and do it, then will print as follows
<UILabel: 0x1cd6c0; frame = (13 0; 25 27); text = 'ON'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1d75f0>>
or like this
<UILabel: 0x179ed0; frame = (101 0; 32 27); text = 'OFF'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x179f40>>
I am not able to get the problem, because the same variable is working showing correct values in other methods, but not working well with
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Why?
In processName:
you do this:
nameOfAlarm = (NSString *)n;
The cast is not necessary but it doesn't hurt. The problem is that you are not retaining n
and it can go away any minute leaving you with a dangling pointer. You need to do this:
self.nameOfAlarm = n; // which is different from nameOfAlarm = n;
The dot notation is just syntactic sugar for:
[self setNameOfAlarm:n];
Since the property is marked retain, the property setter that is created for you via synthesize
will retain the new value of nameOfAlarm
(n
) and release the old value of nameOfAlarm
.
精彩评论