我有一个带有自定义派生单元格的表格视图控制器。当有人单击单元格中的按钮(下面的editButton)时,我想加载一个不同的视图控制器来显示弹出对话框。
我创建了一个xib和视图控制器,并以这种方式加载了新的VC,但新VC对象中的所有内容都是nil,并且没有出现新的VC。
然后我尝试使用initWithDefaultText:origin:initializer创建视图,但它仍然没有出现,但成员窗口小部件现在有效。
我错过了什么吗?
MyListTableViewCell.h@interface MyListTableViewCell : UITableViewCell <MyEditQueryDelegate>
...
MyListTableViewCell.m
@implementation MyListTableViewCell
+ (MyListTableViewCell *)loadFromNibWithItem:(MyListItem *)item
expandHandler:(MyItemExpandHandler)handler
controller:(MyListViewController*)controller
{
MyListTableViewCell *cell = [[[NSBundle mainBundle]
loadNibNamed:@"MyListTableViewCell" owner:self options:nil] objectAtIndex:0];
cell.item = item;
cell.expandHandler = handler;
cell.queryLabel.text = item.displayText;
[cell hideOptions];
[cell.editButton addTarget: cell action: @selector(editButtonTapped:)
forControlEvents: UIControlEventTouchUpInside];
return cell;
}
- (void) editButtonTapped: (UIButton*) sender
{
CGPoint globalOrigin = [self.superview convertPoint:self.frame.origin toView:nil];
NSLog(@"editButtonTapped Point (%f, %f).", globalOrigin.x, globalOrigin.y);
// This log message appears.
// MyEditQueryViewController* vc = [[MyEditQueryViewController alloc]
// initWithNibName:@"MyEditQueryView" bundle:nil];
// This resulted in a nil view in the vc and no visual change.
MyEditQueryViewController* vc = [[MyEditQueryViewController alloc]
initWithDefaultText:[self.queryLabel text] orign:globalOrigin];
vc.delegate = self;
[_controller presentViewController: vc animated: YES completion: nil];
// vc should appear here????
}
MyEditQueryViewController.h
#import <UIKit/UIKit.h>
#import "MyEditQueryDelegate.h"
@interface MyEditQueryViewController : UIViewController
@property (nonatomic, strong) id<MyEditQueryDelegate> delegate;
@property (nonatomic, strong) IBOutlet UITextField* textField;
@property (nonatomic, strong) IBOutlet UIView* editView;
@property (nonatomic, strong) IBOutlet UIButton* cancelButton;
@property (nonatomic, strong) IBOutlet UIButton* saveButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
- (id)initWithDefaultText:(NSString*)defaultText orign:(CGPoint)origin;
- (IBAction)cancelTapped:(id)sender;
- (IBAction)saveTapped:(id)sender;
@end
MyEditQueryViewController.m
@implementation MyEditQueryViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(@"initWithNibName:bundle:");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (id)initWithDefaultText:(NSString*)defaultText orign:(CGPoint)origin
{
NSLog(@"initWithDefaultText:origin:");
self = [super initWithNibName: nil bundle:nil];
if (self)
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
self.view = [[UIView alloc] initWithFrame:screenRect];
UIImage* smokeGlass = [UIImage imageNamed:@"SmokeGlass.png"];
UIImageView* background = [[UIImageView alloc] initWithImage:smokeGlass];
[self.view addSubview:background];
CGRect editViewFrame = CGRectMake(0.0, origin.y, screenRect.size.width, 122.0);
_editView = [[UIView alloc] initWithFrame:editViewFrame];
[self.view addSubview: _editView];
CGRect textViewFrame = CGRectMake(20.0, 20.0, 280.0, 30.0);
_textField = [[UITextField alloc] initWithFrame: textViewFrame];
_textField.text = defaultText;
[_editView addSubview: _textField];
CGRect cancelButtonFrame = CGRectMake(20.0, 72.0, 50.0, 30.0);
_cancelButton = [[UIButton alloc] initWithFrame:cancelButtonFrame];
_cancelButton.titleLabel.text = @"Cancel";
[_cancelButton addTarget:self action:@selector(cancelTapped:)
forControlEvents:UIControlEventTouchUpInside];
[_editView addSubview: _cancelButton];
CGRect saveButtonFrame = CGRectMake(266.0, 72.0, 34.0, 30.0);
_saveButton = [[UIButton alloc] initWithFrame:saveButtonFrame];
_saveButton.titleLabel.text = @"Save";
[_saveButton addTarget:self action:@selector(saveTapped:)
forControlEvents:UIControlEventTouchUpInside];
[_editView addSubview: _saveButton];
}
return self;
}