3

我已经UIPopoverController使用情节提要实现了,但是当我在UITableView.在此处输入图像描述

When select particular row so that time I want to dismiss the popover but I am not able dismiss it.

我为此编写了以下代码:

//在主 UIViewController 中显示弹出框

-(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 中隐藏弹出框

   - (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];
   }
4

4 回答 4

0

在这里做的最聪明的事情(恕我直言)是遵循这个示例代码,我每次都这样做:

// 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];
}
于 2014-06-12T18:05:03.013 回答
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];

}
于 2013-05-12T16:12:29.133 回答
0

以下代码实例化了一个 NEW Instance。所以它与已经存在的 popover 无关:Notepad_ipad *objNote = [[Notepad_ipad alloc] init];

也代替:

popover.delegate = self;
popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];

利用:

popover = [[UIPopoverController alloc] initWithContentViewController:objNoteList];
popover.delegate = self;

即:先分配实例,然后设置它的delegate.

最后替换你的方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

有了这个:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)    indexPath   {    
[popover dismissPopoverAnimated:YES];
}
于 2013-05-10T10:07:22.720 回答
0

利用

 [popover dismissPopoverAnimated:YES];
于 2013-05-10T09:56:47.590 回答