Presenting popover in UITabBarController using UIBarButtonItem iOS9 - objective-c

I would like to present a popover from UIBarButtonItem present in UITabBarController using objectiveC in iOS9. This popover is a UITableViewController. I have coded it in the following way
- (IBAction)MenuButtonPopOverTouch:(id)sender {
LogoutTableViewController* content = [[LogoutTableViewController alloc] init];
content.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:content animated:YES completion:nil];
UIPopoverPresentationController *PopOverPresentation = [content popoverPresentationController];
PopOverPresentation.permittedArrowDirections = UIPopoverArrowDirectionDown;
}
I think i have missed the content size of the popover but don't know how to initialize it. Any help is appreciated. Attaching a preview of my storyboard if needed anything else please let me know

I found answer to my question here is the solution
- (IBAction)MenuPopOver:(id)sender {
[self performSegueWithIdentifier:#"CustomerMenu" sender:self.MenuBarButtonItem];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSString *popoverIdentifier = segue.identifier;
if([popoverIdentifier isEqualToString:#"CustomerMenu"]){
UIViewController *dvc = segue.destinationViewController;
dvc.preferredContentSize = CGSizeMake(150, 50);
UIPopoverPresentationController *ppc = dvc.popoverPresentationController;
if (ppc) {
ppc.delegate = self;
}
}
}
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone;
}
I found it through this link PopoverPresentationController
It has been tested as well

Related

Data Doesn't Load Unless UIButton Clicked

This might be a strange case so I apologize in advance if my problem or solution is unclear:
I have a LoginViewController that fetches a users Facebook profile picture, username, email, etc. I then have it segue to a HomeViewController that displays some objects, other items, and a UIView that shows the users profile picture.
The strange part is that the UIImage that i create only gets transferred when I use a UIButton. I can not seem to get the image to be sent to the HomeViewController any other way. I even set up a GCP to have it try and wait in order for Facebook to deliver the information - still nothing. Below is the code that I have for the LoginViewController.m. If anyone has any idea on why this is happening I would be extremely grateful. thank you!
#import "LoginViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface LoginViewController ()
- (void)toggleHiddenState:(BOOL)shouldHide;
#end
#implementation LoginViewController
#synthesize profPicture;
- (void)viewDidLoad {
[super viewDidLoad];
[self toggleHiddenState:YES];
self.lblLoginStatus.text = #"";
self.loginButton.readPermissions = #[#"public_profile", #"email"];
self.loginButton.delegate = self;
// Do any additional setup after loading the view.
}
-(void)toggleHiddenState:(BOOL)shouldHide{
self.lblUsername.hidden = shouldHide;
self.lblEmail.hidden = shouldHide;
self.profilePicture.hidden = shouldHide;
self.loggedinwallpaper.hidden = shouldHide;
self.FBlogout.hidden = shouldHide;
}
-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView{
self.lblLoginStatus.text = #"";
[self toggleHiddenState:NO];
}
-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView{
self.lblLoginStatus.text = #"";
[self toggleHiddenState:YES];
}
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user{
NSLog(#"%#", user);
self.profilePicture.profileID = user.objectID;
self.lblUsername.text = user.name;
self.lblEmail.text = [user objectForKey:#"email"];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
// Create Facebook Profile Picture from User ID url
NSString *pic_link = [NSString stringWithFormat:#"http://graph.facebook.com/%#/picture?width=300&height=300", user.objectID];
NSURL *pic_url = [NSURL URLWithString:pic_link];
profPicture = [UIImage imageWithData: [NSData dataWithContentsOfURL:pic_url]];
dispatch_async(dispatch_get_main_queue(), ^(void){
//Main Thread : UI Updates
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *homeViewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"HomeViewController"];
[self performSelector:#selector(prepareForSegue:sender:) withObject:nil afterDelay:1.0 ];
[self presentViewController:homeViewController animated:YES completion:nil];
});
});
}
-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error{
NSLog(#"%#", [error localizedDescription]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
HomeViewController *homeviewController = segue.destinationViewController;
homeviewController.homepic = profPicture;
}
#end
From the code you shared I'd assume that the button you mentioned worked because you wired that up in the Storyboard with a segue, right? (Dragged an arrow to the next VC?)
The issue with your code is that here
[self performSelector:#selector(prepareForSegue:sender:) withObject:nil afterDelay:1.0 ];
you are triggering prepareForSegue:sender: on your own without the necessary parameters - you don't have a segue to prepare for as you aren't even segueing to the other view controller. So when HomeViewController *homeviewController = segue.destinationViewController; is executed segue is nil so the image can't be set.
You should be able to fix this by replacing your code which runs on the main thread with this:
dispatch_async(dispatch_get_main_queue(), ^(void){
// Main Thread : UI Updates
[self performSegueWithIdentifier:#"<insert segue identifier here>" sender:self];
});
Make sure to set a name in your Storyboard for the segue between the view controllers and use that identifier here. (See the Apple Docs)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
HomeViewController *homeviewController = segue.destinationViewController;
homeviewController.homepic = [UIImage alloc] init];
homeviewController.homepic = profPicture;
}

Objective-C: How to make a button take you to another class/storyboard

I want to make the bottom button seen in the storyboard on the right to take the user to the signupstoryboard.storyboard/signupController seen on the left when the user taps on the button. How can that be done?
{ - (IBAction)signupbutton:(id)sender; SignupController *signUpView = [self.storyboard instantiateViewControllerWithIdentifier:#"signup"]; [self presentViewController:signUpView animated:YES completion:nil]; }
You'll have to do it by code.
- (IBAction)buttonTapped
{
NSString *storyboardName = #"signupstoryboard";
UIViewController *nextViewController = [[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateInitialViewController];
// if you are using a navigation controller to push it:
[self.navigationController pushViewController:nextViewController animated:YES];
// otherwise if you are presenting it modally:
[self presentViewController:nextViewController animated:YES completion:nil]
}
In your .m (at the top)
#import "signupController.h"
In your button action method (Don't forget to set the storyboard id for your signupController as signup
- (IBAction)signupbutton:(id)sender
{
SignupController *signUpView = [self.storyboard instantiateViewControllerWithIdentifier:#"signup"]; [self presentViewController:signUpView animated:YES completion:nil];
}
Thats it

MGSplitViewController as Modal not working correctly

I am trying to use an MGSplitViewController, but rather than having it set as the rootViewController, I want it to show modally. This is the code I am using so far
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
if(splitViewController == nil)
{
self.splitViewController = [[MGSplitViewController alloc] init];
self.rootViewController = [[RootViewController alloc] init];
self.detailViewController = [[DetailViewController alloc] init];
}
[splitViewController setViewControllers:[NSArray arrayWithObjects:rootViewController, detailViewController, nil]];
if (NO) { // whether to allow dragging the divider to move the split.
splitViewController.splitWidth = 15.0; // make it wide enough to actually drag!
splitViewController.allowsDraggingDivider = YES;
}
splitViewController.delegate = detailViewController;
[rootViewController performSelector:#selector(selectFirstRow) withObject:nil afterDelay:0];
[detailViewController performSelector:#selector(configureView) withObject:nil afterDelay:0];
[self presentViewController:splitViewController animated:YES completion:nil];
}
This works almost fine, but the buttons in the detailViewController do not work. They do nothing. Also, when I click a button in the left panel, the text doesn't change in the detailViewController as it does in the same project.
Am I missing something?
I'm also working with MGSplitViewController, but it sounds to me like you need to set up some communication between your master and detail views. If you look at the demo project, you can see they use delegates to that effect.

Empty Table View in UIPopover

I am having some hard time with TableView insite UIPopover, the table is simply empty, even though it is a static table view.
code:
- (IBAction)settingsBtnPressed:(id)sender {
if(self.pop.isPopoverVisible) {
[self.pop dismissPopoverAnimated:YES];
} else {
//SettingsViewController *s = [[SettingsViewController alloc] init];
SettingsViewController *s = [self.storyboard instantiateViewControllerWithIdentifier:#"settings"];
s.brain = self.brain;
self.pop = [[UIPopoverController alloc] initWithContentViewController:s];
[self.pop setPopoverContentSize:CGSizeMake(400, 500)];
[self.pop presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
}
Attaching screenshots:
Have you implemented methods like cellForRow:atIndex:, numberOfSections:inTableView: and numberOfRows:inSection ?
Maybe you are not using UITableViewController but rather UIViewController in your header file.

UIPopoverController and UIImagePickerController crash

Here is the set up of my view:
When the UIBarButtonItem is clicked, it should bring up a UIImagePickerController. I have to do this using a UIPopoverController, which is invoked by clicking on the "reset" button, because it is necessary on the iPad. Here is my code:
-(IBAction) btnReset:(id)sender {
[self chooseImage];
}
-(void) chooseImage {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
imagepicker = [[UIImagePickerController alloc] init];
imagepicker.allowsEditing = NO;
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagepicker.navigationBar.opaque = true;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagepicker];
[popoverController presentPopoverFromBarButtonItem:reset permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
} else {
[self presentModalViewController:imagepicker animated:YES];
}
}
}
However, when this is called, the view crashes with the error:
'NSInvalidArgumentException', reason: '-[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:]: Popovers cannot be presented from a view which does not have a window.'
What am I doing wrong? Thank you in advance.
It looks like you are trying to create a popover on an item which is not on the view hierarchy. If this method is being invoked by your button then change the method header to -(void) chooseImage:(id)sender and try presenting the popover from the UIBarButton you have on your toolbar.
Also if you are using ARC (which it looks like you are) you have to hold on to your UIPopover otherwise it will be released when it is still required see this stack overflow post. You may already be doing this but I thought I would raise it as a I can't see if/how you have specified your popoverController.