我已经使用故事板实施了UIPopoverController
但是当我在UITableView
中选择特定行时,我无法将其解除。
当选择特定行时,我想要解散弹出窗口,但我无法解除它。
我在下面写下代码:
//在Main UIViewController中显示popover
-(IBAction)clickNotes:(id)sender {
NSLog(@"notes:");
NoteList *objNoteList = [[NoteList alloc] initWithNibName:@"NoteList" bundle:nil];
popover.delegate = self;
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
popover.popoverContentSize = CGSizeMake(250, 450);
[popover presentPopoverFromRect:CGRectMake(730, 0, 1,1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
//在didSelecteRowAtIndexPath上的另一个UIViewController中隐藏popover
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Notepad_ipad *objNote = [[Notepad_ipad alloc] init];
NSString *mSelectedNoteText = @"Selected text";
[objNote SelectedNote:mSelectedNoteText];
[objNote.popover dismissPopoverAnimated:YES];
}
答案 0 :(得分:0)
使用
[popover dismissPopoverAnimated:YES];
答案 1 :(得分:0)
以下代码实例化一个新实例。所以它与现有的popover无关:Notepad_ipad *objNote = [[Notepad_ipad alloc] init];
也代替:
popover.delegate = self;
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
使用:
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
popover.delegate = self;
I.e。:首先分配实例,然后设置其delegate
。
最后替换你的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
用这个:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath {
[popover dismissPopoverAnimated:YES];
}
答案 2 :(得分:0)
//在控制器的.h文件中创建包含didSelectRowAtIndexPath
方法的协议,如下所示:
@protocol Popoverdelegate <NSObject>
{
-(void)didRowAtIndexPathIsSelected;
}
// Add this property in .h file of the same controller
@property (strong, nonatomic) id<Popoverdelegate> delegate;
// Now implement this protocol in interface which calls popovercontroller
// for ex: @interface ViewController <Popovercontroller>
// then add following properties to viewController .h file
@protocol (strong, nonatomic) UIPopoverController *popoverController;
// Implement popoverdelegate protocol in .m file as
- (void) didRowAtIndexPathIsSelected
{
[self.popoverController dismissPopoverAnimated:YES];
}
// Replace your code as follows
-(IBAction)clickNotes:(id)sender
{
NoteList *objNoteList = [[NoteList alloc] initWithNibName:@"NoteList" bundle:nil];
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
popover.delegate = self;
self.popoverController = popover;
self.popoverController.popoverContentSize = CGSizeMake(250, 450);
[self.popoverController presentPopoverFromRect:CGRectMake(730, 0, 1,1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Notepad_ipad *objNote = [[Notepad_ipad alloc] init];
NSString *mSelectedNoteText = @"Selected text";
[objNote SelectedNote:mSelectedNoteText];
[self.delegate dismissPopoverAnimated:YES];
}
答案 3 :(得分:0)
这里最明智的事情(imho)是遵循这个示例代码,我每次都这样做:
// firstViewController.h
@interface firstViewController : UIViewController <SecondDelegate>
{
SecondViewController *choice;
}
// firstViewController.m
- (void)choicePicked(NSString *)choice
{
NSLog(choice);
[_popover dismissPopoverAnimated:YES]; //(put it here)
_popover = nil; (deallocate the popover)
_choicePicker = nil; (deallocate the SecondViewController instance)
}
// secondViewController.h (the one that will give back the data)
@protocol SecondDelegate <NSObject>
- (void)choicePicked:(NSString *)choice;
@end
@interface secondViewController : UITableViewController
@property (nonatomic, assign) id <SecondDelegate> delegate;
@end
// secondViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selection = [_yourArray objectAtIndex:indexPath.row];
[[self delegate] choicePicked:selection];
}