UIButton in PopoverView with own XIB should send action to mainview - xcode

I have a popover view which is called with:
Info *infoview = [[Info alloc]init];
pop = [[UIPopoverController alloc]initWithContentViewController:infoview];
[pop setDelegate:self];
[pop presentPopoverFromRect:CGRectMake(-60, 30, 400, 400) inView:bigMenuView permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[infoview release];
In this popoverview is (see in the code) an info.xib which has some UIBUttons.
Now I want to change something. in my "mainview" after I click on the Button.
Note: I can access my IBAction Method in the "First Responder" but the is no action.

In order to do what you want, declare a property on the button in Info, something like :
#property(retain) IBOutlet UIButton *myButton;
Now synthesize it, in implementation part. In order for the button to send the correct action to the correct target, you should do like :
Info *infoview = [[Info alloc]init];
// set the action/target here
[infoView.myButton addTarget:bigMenuView
action:#selector(theSELYouWant)
forControlEvents:UIControlEventTouchUpInside];
pop = [[UIPopoverController alloc] initWithContentViewController:infoview];
[pop setDelegate:self];
[pop presentPopoverFromRect:CGRectMake(-60, 30, 400, 400)
inView:bigMenuView
permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[infoview release];
I supposed that the main view is bigMenuView.
This will make the button sending a theSELYouWant message to bigMenuView each time it is touched.

Related

Container view popover view programmatically

In iPad View I add Container View, after adding Xcode automatically create new view and connect to my main view with embed segue. Now in code I try to call this new view programmatically and cannot do this, I need to show this form with popover.
I try:
PaymentCashController *newPopup = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
[self presentViewController:newPopup animated:YES completion:nil];
This work but show new view in full screen but I need show with popover. I try many things and all not working (.
I try this:
[self performSegueWithIdentifier:#"PaymentCash" sender:nil];
But receive exception SIGABRT
I try this:
PaymentCashController *newPopup = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:newPopup];
popoverController.popoverContentSize = CGSizeMake(500, 400);
CGRect popoverStartFrame = {50, 500, 100, 100};//Set the frame that you would like your popover to present from.
[popoverController presentPopoverFromRect: popoverStartFrame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
Receive exception SIGABRT
Try this:
PaymentCashController *newPopup = [PaymentCashController new];
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:newPopup];
popoverController = [[UIPopoverController alloc] initWithContentViewController:newPopup];
[popoverController presentPopoverFromRect:CGRectMake(44, 6, 111, 111) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Same SIGABRT
You need to create UIPopoverController with your view controller and present the popover as shown below.
PaymentCashController *newPopup = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
UIPopoverController *popoverController = [[UIPopoverController alloc] newPopup];
popover.delegate = self;
//set custom size you want for popover
popover.popoverContentSize = CGSizeMake(500, 400);
CGRect popoverStartFrame = {50, 500, 100, 100};//Set the frame that you would like your popover to present from.
[popover presentPopoverFromRect: popoverStartFrame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
Many experiments and now all working:
Remove all segue to my popover View in Storyboard. Do it freeform and change size.
Add to my main View new property
#property (nonatomic,strong) UIPopoverController *popOver;
Set storyboard id to my popover View to "PaymentCash".
In any action on my View to show my popover View add this code:
PaymentCashController *PopoverView = [self.storyboard instantiateViewControllerWithIdentifier:#"PaymentCash"];
self.popOver =[[UIPopoverController alloc] initWithContentViewController:PopoverView];
CGRect popoverStartFrame = {1,500,200,1};
[self.popOver presentPopoverFromRect:popoverStartFrame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
Only one problem after this, popover View show in no correct place but this another question.

UIButton event is not firing, even if I connect it through xib

I am creating a calculator game, in which I have to popup a UIAlertView, I customized the UIAlertView and put a UITableView programatically into it, When the user tap any of its cell I show a UIView on the top of UITableView, this view has a button, when I clicked that button the event does not fires, I have also installed it manually, but it doesn't work, previously the attachment of event with button is generating an error, for that I uninstalled the event in dealloc, and fix the problem, but after it still the event is not firing.
i was have case such your and i have to add it pragmatically
in .h declare
UIButton *btn;
in .m add it to the UIView
UIView *TempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
if (btn) {
[btn removeFromSuperview];
// [btn release]; // for not using arc
}
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10, 10, 25, 25)];
[btn addTarget:self action:#selector(ClickMe:) forControlEvents:UIControlEventTouchUpInside];
[TempView addSubview:btn];
[self.view addSubview:TempView];
and the action is
-(void)ClickMe:(id)sender{
//here is an action
}
it's working for me

trying to push a detail view when the right button is tapped on the annotation of mapview in xcode

i wanted to display the detail view after tapping the right button on the annotation of mapview in xcode but i could not get the view to display ,
my code:
- (void)showDetails:(id)sender
{
[self.navigationController setToolbarHidden:YES animated:NO];
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString* BridgeAnnotationIdentifier = #"bridgeAnnotationIdentifier";
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
NSLog(#"%#",rightButton);
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;//[kml viewForAnnotation:annotation];
}
The showDetails method gets called but cannot push the detailViewController to the top ,
[self.navigationController pushViewController:self.detailViewController animated:YES];
this code should have pushed detail view to the top and displayed the detailView but it does not please ,
any help would be greatly appreciated , what i wanted to do is when anybody taps on the right button of annotation on the mapview ,i wanted to display a detail view , thanking you in advance..
There is no target/action required, there is a delegate method that gets called when the callout accessory view is tapped: mapView:annotationView:calloutAccessoryControlTapped:
Thank you , for your help i am able to open the detailViewController by changing my showDetails method
- (void)showDetails:(id)sender
{
detailViewController *dvController = [[detailViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}

Dismiss Multiple popovers xcode

I have a page with multiple popovers that I am trying to dismiss when another popover appears I am using the following code but it doesn't seem to dismiss the popover
-(IBAction)OISButton:(id)sender{
if(![popoverController isPopoverVisible]){
OISView *oisView = [[OISView alloc] init];
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:oisView];
[pop setDelegate:self];
[pop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[pop setPopoverContentSize:CGSizeMake(320, 460)];
[oisView release];
}
else{
// close popover
[popoverController dismissPopoverAnimated:YES];
}
}
Any suggestions on how to modify this code? Thanks
You're not assigning to popoverController, which might explain the problems you're having.
Thanks Tom for putting me in the right direction, I have included the code below for one popover with a view called OISView in a format to handle multiple popovers:
-(IBAction)OISButton:(id)sender{
if([popoverController isPopoverVisible]){
[popoverController dismissPopoverAnimated:YES];
} else
{
OISView *oisView = [[OISView alloc] init];
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:oisView];
self.popoverController = pop;
[pop setDelegate:self];
[pop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[pop setPopoverContentSize:CGSizeMake(320, 460)];
[oisView release];
}
}

Changing the Image for a detail disclosure button (Xcode 4.2)

I have used the following suggested routine to change the detail disclosure button image in a table view cell (in tableView cellForRowAtIndexPath)
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
[myAccessoryButton setBackgroundColor:[UIColor clearColor]];
[myAccessoryButton setImage:[UIImage imageNamed:#"ball"] forState:UIControlStateNormal];
[cell setAccessoryView:myAccessoryButton];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
However, the event tableView accessoryButtonTappedForRowWithIndexPath is now no longer called on clicking the button.
Has anyone any ideas as to why?
I've +1'd Fry's answer, but just to pull the code in from the other site to make SO more complete: The answer is that you have to accessoryButtonTapped.... call will not be made automatically as it would for the built-in detail-disclosure, so you have to do it yourself with the target of the button.
Translating from the link code to your example above, add this line after setImage:
[myAccessoryButton addTarget:self action:#selector(accessoryButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
Then add this later on:
- (void)accessoryButtonTapped:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
if (indexPath != nil) {
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
(copied verbatim from this link)
Note that this assumes that the button is created in the UITableViewController (in my case I'm creating one in a custom cell so the references are slightly different)
Explanation: the accessoryButtonTapped is a custom target method for our custom button. When the button is pressed ("TouchUpInside") then we find the indexPath of the cell at the point in which the press occurred, and invoke the method that the table view would ordinarily invoke.

Resources