【发布时间】:2011-11-11 16:55:54
【问题描述】:
我有一个对象,我 init 喜欢这样:
pdtd = [[ProfileDataTableDelegate alloc] initWithTableView:profileDataTableView scrollView:contentScrollView];
所以我的view 被传递给pdtd 对象。我现在可以毫无问题地注销我的观点。当我在文本字段内单击并告知滚动视图重新定位时,我得到 错误。在我的表委托 (pdtd) 上,我收到了错误,该错误在该函数的第一行给出了“EXC_BAD_ACCESS”:
- (void)keyboardDidShow:(NSNotification *)notif {
[self.scrollView setContentOffset:CGPointMake(0, 100) animated:YES]; // <-- ERROR SHOWS UP HERE
[self.scrollView setContentSize:CGSizeMake([self.scrollView frame].size.width,
[self.scrollView frame].size.height + 100)];
}
这很奇怪,因为我在另一个视图上执行此操作并且效果很好。我不确定出了什么问题或如何从调试器中获取更多信息。下面是相关类中的相关函数。
ProfileViewController.h
...
#import "ProfileDataTableDelegate.h"
@interface ProfileViewController : UIViewController <UINavigationControllerDelegate> {
UITableView *profileDataTableView;
ProfileDataTableDelegate *pdtd;
UINavigationBar *navBar;
UIScrollView *contentScrollView;
}
...
@property (nonatomic, retain) IBOutlet UITableView *profileDataTableView;
@property (nonatomic, retain) ProfileDataTableDelegate *pdtd;
@property (nonatomic, retain) IBOutlet UINavigationBar *navBar;
@property (nonatomic, retain) IBOutlet UIScrollView *contentScrollView;
@end
ProfileViewController.m
...
- (void)viewDidLoad {
[super viewDidLoad];
//nav title
[navBar.topItem setTitle:@"Your Profile"];
[self.navigationController setNavigationBarHidden:NO];
// nav bar button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(saveBtnTouched:)];
saveButton.enabled = false;
[navBar.topItem setRightBarButtonItem:saveButton];
pdtd = [[ProfileDataTableDelegate alloc] initWithTableView:profileDataTableView scrollView:contentScrollView];
void (^block)(BOOL) = ^(BOOL is_valid) {
if(is_valid == YES) {
[saveButton setEnabled:YES];
} else if(is_valid == NO) {
[saveButton setEnabled:NO];
}
};
[pdtd setValidatorBlock:block];
}
...
ProfileDateTableDelegate.h
...
#import "ValidatedTextViewTableCell.h"
typedef void (^ ValidatorBlock)(BOOL);
@interface ProfileDataTableDelegate : NSObject <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate> {
UITableView *profileDataTableView;
UIScrollView *scrollView;
...
}
- (id)initWithTableView:(UITableView *)tableView scrollView:(UIScrollView *)scrollView;
...
@property (nonatomic, retain) IBOutlet UITableView *profileDataTableView;
@property (nonatomic, retain) UIScrollView *scrollView;
@end
ProfileDateTableDelegate.m
...
@implementation ProfileDataTableDelegate
@synthesize profileDataTableView;
@synthesize profileDataTableLabels;
@synthesize scrollView;
@synthesize emailCell, phoneCell, nameCell;
@synthesize validatedTextViewTableCell;
@synthesize validatorBlock;
- (id)initWithTableView:(UITableView *)tableView scrollView:(UIScrollView *)view {
self = [super init];
if(self) {
profileDataTableView = tableView;
[profileDataTableView setDelegate:self];
[profileDataTableView setDataSource:self];
scrollView = view;
profileDataTableLabels = [[NSArray alloc] initWithObjects:@"Name", @"Email", @"Phone", nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector (keyboardDidShow:)
name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector (keyboardDidHide:)
name: UIKeyboardDidHideNotification object:nil];
}
return self;
}
...
- (void)keyboardDidShow:(NSNotification *)notif {
//NSLog(@"keyboard did show %@", notif); // notif gives coordinates etc
//[scrollView setContentOffset:CGPointMake(0, 480 - 372) animated:YES];
[self.scrollView setContentOffset:CGPointMake(0, 100) animated:YES];
[self.scrollView setContentSize:CGSizeMake([self.scrollView frame].size.width,
[self.scrollView frame].size.height + 100)];
//[scrollView setFrame:CGRectMake(0, 0, 320, 320)];
// disable scrolling because it resets the rect position
//[scrollView setScrollEnabled:NO];
}
...
@end
我不确定如何获取堆栈跟踪信息 (Xcode 4.2)。此外,在分析中有几个警告,但没有什么严重的(例如蓝色)。
【问题讨论】:
标签: objective-c ios