load custom view in MBProgressHUD - uiview

Is it possible to load a custom view in MBProgressHUD overlay, there is a custom view option with the MBProgressHUD class, but can i assign a XIB view etc with customView??

Yes you could do something like this:
MBProgressHUD *loadingHUD = [[MBProgressHUD alloc] init];
loadingHUD.mode = MBProgressHUDModeCustomView;
loadingHUD.labelText = nil;
loadingHUD.detailsLabelText = nil;
UIView *customView = [[UIView alloc] initWithFrame:self.view.bounds]; // Set a size
// Add stuff to view here
loadingHUD.customView = customView;
[HUD show:YES];

Related

UIPopoverController and UITapGestureRecognizer not working in ViewDidLoad

When I add a UIPopoverController in the ViewDidLoad method, it displays an empty popover view. The same thing happens when I add a UITapGestureRecognizer; it doesn't trigger the attached method. If I put the code in my ViewDidAppear it does work, but in some cases ViewDidAppear is never called, depending on where in my storyboard the view is loaded.
Here's my code:
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor whiteColor];
popoverContent.view = popoverView;
pickerPopup = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
pickerPopup.delegate = (id)self;
[pickerPopup setPopoverContentSize:CGSizeMake(320, 240) animated:NO];
And the UITapGestureRecognizer:
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openPicker:)];
[_form_from addGestureRecognizer:tap];
I solved it for now using a delay in the ViewDidLoad method like so
[self performSelector:#selector(custom_init) withObject:nil afterDelay:0.1];
Still don't get why the UITapGestureRecognizer doesn't trigger. It does work if I attach it to the view, but not if I attach it to a textfield in a static cell.

Programmatically create a UINavigationController with a UICollectionViewController embed in a container

What I'd like to do is create a navigation controller based on a layout. Therefor I created a method renderLayout in my UIStoryboard class.
renderLayout:
UINavigationController *navigationController = [[UINavigationController alloc] init];
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
/* navigation bar styling */
// navigationController.toolbar.translucent = false;
// navigationController.title = #"TEST";
UIViewController *controller = [[UIViewController alloc] init];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
scrollView.contentSize = CGSizeMake(1024, /* some calculated value here */);
/* controller styling */
//controller.title = [page valueForKey:#"title"];
NSArray *items = /* ... */
int containerHeight = (1 + ([items count] / 3)) * 29;
MultiSelectionViewController *multiSelection = [[MultiSelectionViewController alloc] initWithItems:[data valueForKey:#"items"]];
multiSelection.view.frame = CGRectMake(20, 65, 984, containerHeight);
/* for debugging */
//multiSelection.view.backgroundColor = [UIColor redColor];
[scrollView addSubview:multiSelection.view];
[controller addChildViewController:multiSelection];
[multiSelection didMoveToParentViewController:controller];
[view addSubview:scrollView];
[controller.view addSubview:view];
[viewControllers addObject:controller];
[navigationController pushViewController:controller animated:true];
return navigationController;
The MultiSelection is a UICollectionViewController<UICollectionViewDataSource> kind of class that creates a collection of items which can be selected.
MultiSelectionViewController init method:
- (id)initWithItems:(NSArray*)items
{
self = [super init];
if (self) {
self.items = items;
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
self.view = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 0, 984, 29) collectionViewLayout:layout];
self.collectionView.dataSource = self;
}
return self;
}
After a uses pushes a button on that current UIViewController the method gets called. It should then construct the navigation controller which I return and later load with
[self presentViewController:renderedNavController animated:true completion:^(){}];
So far everything is working quite well. The one problem I have is that It displays all kinds of labels and buttons and stuff that I can directly push onto the scrollView but it fails to load the MultiSelection elements. It displays a red rectangle in the size of the view frame but viewDidLoad never gets called inside the MultiSelectionViewController. Can someone help me with this? I've already done a lot of google research but couldn't find any solution yet. I hope I explained my problem thoroughly, if not, ask away :)
Okay I finally found the answer. In order to have the viewDidLoad method called I have to provide a layout and init the view controller with it.
Hence self = [super init]; is wrong, the correct way to do it is self = [super initWithCollectionViewLayout:layout
On a side note, I had to use UICollectionViewFlowLayout (instead of UICollectionViewLayout) and use the method setItemSize to provide the correct size of the cells.

MBProgressHUD custom view doesn't appear

I'm trying to show an image on MBProgressHUD by using this code
MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"37x-Checkmark"]] autorelease];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = #"تم إرسال وزنك بنجاح";
[HUD show:YES];
[HUD hide:YES afterDelay:1.5];
but this is what i get
what is the problem ?
I tried your code and it is working for me.
In MBProgressHud.h file, in comments it is mentioned that
/**
* The UIView (i.g., a UIIMageView) to be shown when the HUD is in MBProgressHUDModeCustomView.
* For best results use a 37 by 37 pixel view (so the bounds match the build in indicator bounds).
*/
So perhaps the image you used is missing or is noyt included. Please check that.
You must include the images from this link in your xcode project, seems you didn't do so.
That's because you don't have the image named "37x-Checkmark" in your project. Just add the 37x-Checkmark.png image file in your project.
Now it is changed with IOS &
-(IBAction)save:(id)sender
{
HUD = [[MBProgressHUD alloc] initWithWindow:[[UIApplication sharedApplication] keyWindow]];
[[[UIApplication sharedApplication] keyWindow] addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Initiating Entropy...";
HUD.minSize = CGSizeMake(135.f, 135.f);
[HUD showWhileExecuting:#selector(myTask) onTarget:self withObject:nil animated:YES];
}
-(void)myTask
{
sleep(6);
UIView *v = [[UIView alloc] init];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"check_mark_green"]];
[v setFrame:CGRectMake(0, 0, img.frame.size.width, img.frame.size.height)];
v.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"37x-Checkmark"]];
HUD.mode = MBProgressHUDModeCustomView;
HUD.customView = v;
HUD.labelText = nil;
HUD.detailsLabelText = #"Geo-Location Cordinates Secured";
}
try this and enjoy the code.

How to display a popover programmatically from a uibutton that is also created programmatically (Not using interface builder)

I have a button I have created programmatically within a view controller. Once the button is pressed I want it to to use a method to create the popover programmatically.
The button which is created in the ViewDidLoad in my view controller.m
UIView *moreFundInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 540, 620)];
[self.view addSubview:moreFundInfoView];
[moreFundInfoView setBackgroundColor:[UIColor RMBColor:#"b"]];
btnContact = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
[btnContact setFrame:CGRectMake(390, 575, contactButton.width, contactButton.height)];
btnContact.hidden = NO;
[btnContact setTitle:#"Contact" forState:(UIControlStateNormal)];
[moreFundInfoView addSubview:btnContact];
[btnContact addTarget:self action:#selector(showContactDetails:) forControlEvents:UIControlEventTouchUpInside];
Then I have the method I use when the button is pressed.
-(void) showContactDetails: (id) sender
{
UIViewController *popoverContent = [[UIViewController alloc]init];
UIView *popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
[popoverView setBackgroundColor:[UIColor RMBColor:#"b"]];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 300);
UIPopoverController *contactPopover =[[UIPopoverController alloc] initWithContentViewController:popoverContent];
[contactPopover presentPopoverFromRect:btnContact.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES ];
[contactPopover setDelegate:self];
}
What am I missing here? Cause it runs fine, but as soon as I click the button the app crashes. I think it is a delegate issue, but I am not sure. Any advice would be appreciated.
I think this code will help you. You are certainly missing delegate methods
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
popover.delegate = self;
popover.popoverContentSize = CGSizeMake(644, 425); //your custom size.
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES];
Just make sure you are not forgetting UIPopover Delegate methods or else application will definitely crash. It is mandatory.
UIViewController *controller = [[UIViewController alloc] init];
[view removeFromSuperview]; //view is a view which is displayed in a popover
controller.view = view;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:controller];
popover.delegate = self;
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
All I had to do was change the property from "retain" to "strong" in the .h file and it works, stopped the app from crashing.
Yes, changing property for "retain" to "strong" makes you to hold your picker view object.
I think the problem with your code was, UIPopoverController object gets deallocated automatically when method finishes.
making strong property gets you to strongly point an object.

Create uiTabBarController programmatically

I want to create a UIView for a UITabBarController
Here is my code for the .h file :
#interface TE : UIViewController <UITabBarControllerDelegate>{
UITabBarController *tabBarController;
}
#property (nonatomic,retain) UITabBarController *tabBarController;
#end
The viewDidLoad method:
UIViewController *testVC = [[T1 alloc] init];
UIViewController *otherVC = [[T2 alloc] init];
NSMutableArray *topLevelControllers = [[NSMutableArray alloc] init];
[topLevelControllers addObject: testVC];
[topLevelControllers addObject: otherVC];
tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
[tabBarController setViewControllers:topLevelControllers animated:NO];
tabBarController.selectedIndex = 0;
self.view = tabBarController.view;
This creates the tab bar controller, but when I click on a tab bar item, I get an error:
Thread1:Program receive signal: SIGABRT
Edit: I solved the problem by downloading and modifying the version of http://www.iphonedevcentral.com/create-uitabbarcontroller/
You say above that you don't want to create the tabBarController in the appDelegate. Why not? Where else would you create it? The tabBarController has to be the root view controller and cannot be a child of any other view controller.
Btw, make sure you implement:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSUInteger tabIndex = [tabBarController.viewControllers indexOfObject:viewController];
if (viewController == [tabBarController.viewControllers objectAtIndex:tabIndex] ) {
return YES;
}
return NO;
}
Subclass UITabBarController
Override the - (void) loadView method and include the following code
MyCustomViewControllerOne* ctrl1 = [[[MyCustomViewControllerOne alloc] initWithNibName#"MyViewControllerOne" bundle: nil] autorelease];
UIViewController* ctrl2 = [[[UIViewController alloc] init] autorelease];
MyCustomControllerTwo* ctrl3 = [[[UIViewController alloc] initWithObject: myObj] autorelease];
ctrl1.title = #"First tab";
ctrl2.title = #"Second tab";
ctrl3.title = #"Third tab";
ctrl1.tabBarItem.image = [UIImage imageNamed:#"tab_image1.png"];
ctrl2.tabBarItem.image = [UIImage imageNamed:#"tab_image2.png"];
ctrl3.tabBarItem.image = [UIImage imageNamed:#"tab_image3.png"];
[self setViewControllers: #[ctrl1, ctrl2, ctrl3]];
That's pretty much it.
Change self.view = tabBarController.view; to
[self.view addSubview:tabBarController.view]; And it works correctly
Trying changing
self.view = tabBarController.view;
to
[self.view addSubview:tabBarController.view];
See if that helps.
Also try placing this in your -(void)loadView method
- (void)loadView {
UIView *mv = [[UIView alloc] initWithFrame:CGRectMake(0.0, 100.0, 320.0, 480.0)];
self.view = mv;
[mv release];
}
The reason you probably are experiencing a black screen is because you haven't initialized your UIView properly.
#Mehdi, just make your TE a UITabBarController instead of a UIViewController which then has a TabBarController in it. Makes it all the more easy to manage your TabBarController. To respond to some others who have indicated that you can have only one TabBarController as the window's rootViewController. That is not the case. A UITabBarController can be instantiated in multiple places where you need a second level menu navigation. Have a TabBar within a TabBar would not make sense, but having a left Navigation Menu and then having a TabBar on each menu item would make sense.

Resources