How do I call the 'popoverControllerDidDismissPopover' method? - ios

I am having trouble calling the the popoverControllerDidDismissPopover method in as much as I do not know where to put it and how to call it.
I have created a popover as follows -
// SettingsViewController.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ViewController.h"
#import "SharedData.h"
#import "PlayerPopUpVC.h"
#interface SettingsViewController : UIViewController <UITableViewDataSource, UIPopoverControllerDelegate> {
- (IBAction)popUp:(id)sender;
#property (strong, nonatomic) UIPopoverController *playerPopUpVC;
#property (strong, nonatomic) PlayerPopUpVC *popUp;
// SettingsViewController.m
#import "SettingsViewController.h"
- (IBAction)popUp:(id)sender {
UIButton *editPlayers = (UIButton *)sender;
if(self.playerPopUpVC) {
self.popUp= [[PlayerPopUpVC alloc] initWithNibName:#"PlayerPopUpVC" bundle:nil];
self.popUp=[[UIPopoverController alloc] initWithContentViewController:self.popUp];
}
[self.playerPopUpVC presentPopoverFromRect:[editPlayers frame] inView:[editPlayers superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
I know I have to set the delegate of my PopOver to self in order to call the method but cannot work out what the code is.
I have tried -
self.playerPopUpVC.delegate=self
but Xcode does not like it.
My popOver class looks like this -
// PlayerPopUpVC.h
#import <UIKit/UIKit.h>
#interface PlayerPopUpVC : UIViewController <UITableViewDataSource, UIPopoverControllerDelegate> {
}
// PlayerPopUpVC.m
#import "PlayerPopUpVC.h"
#interface PlayerPopUpVC ()
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.modalInPopover = NO;
self.contentSizeForViewInPopover = CGSizeMake(240, 400);
}
Any help would be most welcome. I have spent a week now trying to sort it.

First, you need to understand the delegate pattern, which seems that you dont fully understand yet.
The popover will be the one which will call the popoverControllerDidDismissPopover method on the delegate. You only have to implement the UIPopoverControllerDelegate protocol in your class and assign yourself as the delegate of the popover. Why do you say that XCode doesn't like it? please, provide more info.
Furthermore, you are making an incorrect assignment here:
self.popUp=[[UIPopoverController alloc] initWithContentViewController:self.popUp];
Edit: Provided more code to help with the error. Please, review the delegate pattern next time before making these questions.
Your SettingsController.m should have this instead:
- (IBAction)popUp:(id)sender {
UIButton *editPlayers = (UIButton *)sender;
if(!self.popUp) {
self.popUp= [[PlayerPopUpVC alloc] initWithNibName:#"PlayerPopUpVC" bundle:nil];
}
self.playerPopUpVC=[[UIPopoverController alloc] initWithContentViewController:self.popUp];
self.playerPopUpVC.delegate = self;
[self.playerPopUpVC presentPopoverFromRect:[editPlayers frame] inView:[editPlayers superview] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
// Your code here
}

Related

Objective C Transparent UIViewController for Gameover Screen

Information: I would like to make a little Game (Not Important what kind of game) with a little Game Over Screen.
Because i cant Upload Images, i try to Describe it.
You Play, everything is alright. Then you get Gameover. The Screen should be the Same and an Overlayer is Showing the Score (Transparent / Alpha)
Now my First Question is: Is my Solution good?
I use one UIViewController from the Storyboard
Source:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIButton *btnTest;
}
-(IBAction)btnTestClick;
#end
ViewController.m
#import "ViewController.h"
#import "MeinGottEh.h"
#interface ViewController ()
#end
#implementation ViewController
-(void)btnTestClick
{
self.modalPresentationStyle = UIModalPresentationCurrentContext;
MeinGottEh *sampleView = [[MeinGottEh alloc] init];
sampleView.labelText = #"High: 123";
[sampleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:sampleView animated:YES];
}
#end
Then the Second UIViewController (External File, not in Storyboard)
MeinGottEh.h
#import <UIKit/UIKit.h>
#interface MeinGottEh : UIViewController
{
IBOutlet UIButton *btnClose;
}
-(IBAction)btnCloseClick;
#property(nonatomic) NSString *labelText;
#end
MeinGottEh.m
#import "MeinGottEh.h"
#interface MeinGottEh ()
#end
#implementation MeinGottEh
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
[btnClose setTitle:self.labelText forState:UIControlStateNormal];
}
-(void)btnCloseClick
{
[self dismissModalViewControllerAnimated:YES];
}
#end
Is this Solution to Open a Highscore Box good? If no, please tell me how to do it (I make a Research on Stackoverflow. Thats the Most Parts)
If Yes: How can i make it Transparent?
Thank you very much.

delegate respondsToSelector:selector not working

I saw a lot of this kind of questions and answers here, but couldn't find solution to my problem. I'm trying to send data from one view controller to another and use delegate. But don't know why my postDelegate doesn't responds to selector. Is something wrong with this code or what is the problem?
PostViewController.h file
#protocol GetDataDelegate <NSObject>
-(void)getPassedInfo:(NSString*)info;
#end
#interface PostViewController : UIViewController
#property (nonatomic, weak) id <GetDataDelegate> postDelegate;
#end;
PostViewController.m file
#import "PostViewController.h"
- (IBAction)postData:(id)sender {
if ([_postDelegate respondsToSelector:#selector(getPassedInfo:)]) {
[self.postDelegate getPassedInfo:#"data"];
NSLog(#"responds");
}
[self dismissViewControllerAnimated:YES completion:nil];
}
in second view controllers .h file
#import "PostViewController.h"
#interface MainViewController : UITableViewController <GetDataDelegate>
and in .m file
#implementation MainWindowTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
PostViewController * postController = [[PostViewController alloc]init];
postController.postDelegate = self;
}
and here is delegate method:
-(void)getPassedInfo:(NSString *)info{
NSLog(#"info is %#", info);
}
You need to make postController a property or an ivar. Currently it is a local variable in the viewDidLoad method which will be deallocated after viewDidLoad completes as #CodaFi said above.
#import "PostViewController.h"
#interface MainViewController : UITableViewController <GetDataDelegate>
#property (nonatomic, strong) PostViewController *postController;
#end
Then:
- (void)viewDidLoad
{
[super viewDidLoad];
self.postController = [[PostViewController alloc]init];
self.postController.postDelegate = self;
}

UITableView's custom delegate doesn't get called when displayed from UIPopoverController

Hoping someone had to solve related issues .. this is driving me nuts :/
My UITableViewController implements a custom delegate method:
.h
#protocol folderDelegate
#required
- (void)folderViewDidSelectPlan:(NSString*)planId;
#end
#interface FolderViewController : UITableViewController
#property (nonatomic, assign) id delegate;
#end
.m
#implementation FolderViewController
#synthesize delegate;
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSDictionary *row = [self->resultsPlan objectAtIndex:indexPath.row];
if ([delegate respondsToSelector:#selector(folderViewDidSelectPlan:)]) {
[delegate folderViewDidSelectPlan:[row objectForKey:#"id"]];
}
}
In my iPad's MainView I'm displaying this UITableView via UIPopoverController:
#interface ProjectViewController ()<folderDelegate>
...
- (void) selectPlan:(UIBarButtonItem*)sender
{
if([self->popoverSelectPlanController isPopoverVisible]){
[self->popoverSelectPlanController dismissPopoverAnimated:YES];
}
FolderViewController *folder = [[FolderViewController alloc] initWithStyle:UITableViewStyleGrouped withInstallation:self->_installationId withProjectId:self->_projectId withParentFolderId:#""];
folder.delegate = self;
UINavigationController *folderNavView = [[UINavigationController alloc] initWithRootViewController:folder];
self->popoverSelectPlanController = [[UIPopoverController alloc] initWithContentViewController:folderNavView];
[self->popoverSelectPlanController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
And handling the delegate via:
- (void) folderViewDidSelectPlan:(NSString *)planId
{
NSLog(#"called");
}
However, folderViewDidSelectPlan never get's called - I'm really stuck here, hope anyone has an idea how to solve this.
Thanks a lot!
Try to change declaration of the property to:
#property (assign) id<folderDelegate> delegate;
And also use self.delegate instead of in your UITableViewController.m file every time instead of just delegate.
If you don't have to support iOS4 or less remove synthesise from UITableViewController.m.

iOS Access parentViewController to change the State of a UIButton setSelected:YES

I have a UIButton in MainViewController.
MainViewController has a childViewContoller.
I need to access the UIButton (tcButton) property in MainViewController FROM the childViewController and set it to setSelected:YES in viewDidLoad. I have the following code in my ChildViewController.m file and it's not working.
#import "ChildViewController.h"
#import "MainViewController.h"
#import "CoreData.h"
#interface ChildViewContoller ()
#property (nonatomic, strong) CoreData *coreData;
#property (nonatomic, strong) MainViewController *mainViewController;
#end
#implementation ChildViewController
#synthesize coreData, mainViewController;
-(void)viewDidLoad
{
[super viewDidLoad];
self.managedObjectContext = [(STAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
[[(mainViewController *)self.parentViewController tcButton] setSelected:YES];
}
Your code is kind of a mess. Why are you creating a new instance of yourself in viewDidLoad? This makes no sense. If ChildViewController is truly a child view controller, then you can access the parent with self.parentViewController. You only need one line in the viewDidLoad:
-(void)viewDidLoad // Line 4
{
[[(MainViewController *)self.parentViewController tcButton] setSelected:YES]; // Line 8
}
There are several issues in your code but the main idea to perform what you want is getting a pointer to the mainViewController. There are many ways to do that but here a simple example how you can implement such thing. For instance in the initializer of the ChildViewContoller you can pass a pointer to the mainViewController:
#interface ChildViewContoller ()
#property (nonatomic, strong) MainViewController *mainViewController;
#end
#implementation ChildViewContoller
- (id)initWithMainViewController:(MainViewController *)mainViewController
{
self = [super init];
if (self)
{
_mainViewController = mainViewController;
}
return self;
}
- (void)viewDidLoad
{
[_mainViewController.tcButton setSelected:YES];
}
#end
Please not that I have not tested the code above but you can get the idea.

IOS5 iPad popup controller delegate not sending messages to parent controller

I'm new to iOS programming and I can't get this simple concept to work -- I just want the popup controller to be able to call a method and send data on the parent controller. Can anyone spot what I'm doing wrong here?
in DetailViewController.h
#import <UIKit/UIKit.h>
#import "Employee.h"
#import "CompleteViewController.h"
#class EmployeesTVC;
#interface DetailViewController : UIViewController <UISplitViewControllerDelegate, UIPopoverControllerDelegate>
#property (strong) UIPopoverController *popController;
-(IBAction)completeButtonPressed:(id)sender;
#end
in DetailViewController.m (took out irrelevant parts)
#implementation DetailViewController {
__weak UIPopoverController *completePopover;
}
// ...
#pragma mark - Complete / Score popover methods
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(#"preparing for segue");
UIStoryboardPopoverSegue *popoverSegue = (UIStoryboardPopoverSegue *)segue;
UIPopoverController *scorePopoverController = [popoverSegue popoverController];
[scorePopoverController setDelegate:self];
self.popController = scorePopoverController;
}
- (IBAction)completeButtonPressed:(id)sender {
if (completePopover) {
[completePopover dismissPopoverAnimated:YES];
} else {
[self performSegueWithIdentifier:#"showScorePopover" sender:sender];
}
}
- (void) scoreAssigned:(NSString *)score {
NSLog(score);
NSLog(#"Score Assigned");
}
// ...
#end
CompleteViewController.h (the popup view controller)
#import <UIKit/UIKit.h>
#protocol CompleteViewDelegate <NSObject>
- (void)scoreAssigned:(NSString *)score;
#end
#interface CompleteViewController : UIViewController
#property (nonatomic, assign) id<CompleteViewDelegate> delegate;
- (IBAction)okButtonPressed:(id)sender;
#end
CompleteViewController.m
#import "CompleteViewController.h"
#implementation CompleteViewController
#synthesize delegate;
- (IBAction)okButtonPressed:(id)sender {
NSLog(#"OK Button Pressed");
[delegate scoreAssigned:#"100"];
}
#end
Is your first NSLog statement firing? "OK Button Pressed". If not, make sure (IBAction)okButtonPressed is wired up in Interface Builder. If so, but a break point on [delegate scoreAssigned:#"100"]; and hover over "delegate" with your mouse to see if it's nil, just to see if the delegate was successfully assigned. If this doesn't fix it, let us know exactly where you're getting to before things stop working.
Properly presenting a UIPopoverController
[completePopover setDelegate:self];
[completePopover presentPopoverFromBarButtonItem:yourButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
//or if you don't have a bar button item
[completePopover presentPopoverFromRect:CGRectMake(0.0, 0.0, 0.0, 0.0) inView:yourTargetView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Keep in mind that inView can be any subclass of UIView, like UIButton for instance.

Resources