ios tabbar customization with images - objective-c

i'm working in ios application i need to customize tabbar to be like this
First I created 5 viewcontrollers each one in navigation controller
then put them in tabbbarcontroller
I googled for this problem and I found solution
[self.tabBarItem setFinishedSelectedImage:<#(UIImage *)#> withFinishedUnselectedImage:<#(UIImage *)#>]
but it is for iOS 5, I need solution for both iOS 4 and iOS 5.

For customizing tab bar in ios4 is not available with code for that you need to make us custom tab bar for that you can refer this Que.
How to Customize the tabbarcontroller
or you also can do like simple logic with making full tab bar image like this
here i have made one image view on appdel did finish method and done like this in the app.
self.imgV.frame=CGRectMake(0, 431, 320, 49);
[self.tabbarcontroller.view addSubview:self.imgV];
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
NSString *deviceType = [UIDevice currentDevice].model;
NSLog(#"Device%#",deviceType);
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
// self.imgV.frame=CGRectMake(0, 975, 768, 49);
//[self.tabbarcontroller.view addSubview:self.imgV];
switch (index) {
case 0:
self.imgV.image=[UIImage imageNamed:#"reservation_tab~iPad.png"];
break;
case 1:
self.imgV.image=[UIImage imageNamed:#"place_order_tab~iPad.png"];
break;
case 2:
self.imgV.image=[UIImage imageNamed:#"location_tab~iPad.png"];
break;
case 3:
self.imgV.image=[UIImage imageNamed:#"favorite_tab~iPad.png"];
break;
case 4:
self.imgV.image=[UIImage imageNamed:#"signature_dishes_tab~iPad.png"];
break;
case 5:
self.imgV.image=[UIImage imageNamed:#"history_tab~iPad.png"];
break;
case 6:
self.imgV.image=[UIImage imageNamed:#"contact_us_tab~iPad.png"];
break;
default:
break;
}
}
else{
switch (index) {
case 0:
self.imgV.image=[UIImage imageNamed:#"reservation_tab.png"];
break;
case 1:
self.imgV.image=[UIImage imageNamed:#"place_order_tab.png"];
break;
case 2:
self.imgV.image=[UIImage imageNamed:#"location_tab.png"];
break;
case 3:
self.imgV.image=[UIImage imageNamed:#"favorite_tab.png"];
break;
case 4:
self.imgV.image=[UIImage imageNamed:#"gallery_tab.png"];
break;
default:
break;
}
}
return YES;
}

In the AppDelegate.m file, add the following code. In that piece of code, we are creating four views and adding them to a Tab Controller. These views are empty for now because we don’t need any content in them for the purposes of this project.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[UIViewController alloc] init];
UIViewController *viewController2 = [[UIViewController alloc] init];
UIViewController *viewController3 = [[UIViewController alloc] init];
UIViewController *viewController4 = [[UIViewController alloc] init];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2,
viewController3,
viewController4, nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
return YES;
}
You can see a good tutorial here

try this
paste it .h file
#import <UIKit/UIKit.h>
#class MapViewController,MenuViewController;
#interface UITabBar (ColorExtensions)
- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
#end
#interface UITabBarItem (Private)
#property(retain, nonatomic) UIImage *selectedImage;
- (void)_updateView;
#end
#interface SegmentedControlExampleAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow * window;
UINavigationController * navigationController;
NSMutableArray *breads;
NSMutableArray *categorys;
NSMutableArray *collections;
NSString *databaseName;
NSString *databasePath;
MapViewController *mapViewController;
MenuViewController *wvTutorial;
}
#property (nonatomic, retain) IBOutlet UIWindow * window;
#property (nonatomic, retain) UINavigationController * navigationController;
#property (nonatomic,retain) NSMutableArray *breads;
#property (nonatomic,retain) NSMutableArray *categorys;
#property (nonatomic,retain) NSMutableArray *collections;
#property (nonatomic, retain) UITabBarController *tabBarController;
#property (nonatomic, retain) MenuViewController *wvTutorial;
#end
In .m file
#import "SegmentedControlExampleAppDelegate.h"
#import "SegmentManagingViewController.h"
#import "sqlite3.h"
#import "AtoZHomePageViewController.h"
#import "CategoryViewHomePage.h"
#import "CollectionsListHomePageViewController.h"
#import "AboutUs.h"
#import "StoreLocatorViewController.h"
#import "UINavigationBar+CustomImage.h"
#import "MenuViewController.h"
#implementation UITabBar (ColorExtensions)
- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur
{
CGColorRef cgColor = [color CGColor];
CGColorRef cgShadowColor = [shadowColor CGColor];
for (UITabBarItem *item in [self items])
if ([item respondsToSelector:#selector(selectedImage)] &&
[item respondsToSelector:#selector(setSelectedImage:)] &&
[item respondsToSelector:#selector(_updateView)])
{
CGRect contextRect;
contextRect.origin.x = 0.0f;
contextRect.origin.y = 0.0f;
contextRect.size = [[item selectedImage] size];
// Retrieve source image and begin image context
UIImage *itemImage = [item image];
CGSize itemImageSize = [itemImage size];
CGPoint itemImagePosition;
itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
UIGraphicsBeginImageContext(contextRect.size);
CGContextRef c = UIGraphicsGetCurrentContext();
// Setup shadow
CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
// Setup transparency layer and clip to mask
CGContextBeginTransparencyLayer(c, NULL);
CGContextScaleCTM(c, 1.0, -1.0);
CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);
// Fill and end the transparency layer
CGContextSetFillColorWithColor(c, cgColor);
contextRect.size.height = -contextRect.size.height;
CGContextFillRect(c, contextRect);
CGContextEndTransparencyLayer(c);
// Set selected image and end context
[item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
// Update the view
[item _updateView];
}
}
#end
#implementation SegmentedControlExampleAppDelegate
#synthesize window,tabBarController, navigationController,breads,categorys,collections,wvTutorial;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
databaseName = #"ProductsConnect_Master.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
self.tabBarController = [[UITabBarController alloc] init];
UIViewController *viewController = [[AboutUs alloc] initWithNibName:#"AboutUs" bundle:nil];
UIViewController *viewController2 = [[StoreLocatorViewController alloc] initWithNibName:#"StoreLocatorViewController" bundle:nil];
//UIViewController *viewController3 = [[MenuViewController alloc] initWithNibName:#"MenuViewController" bundle:nil];
self.wvTutorial = [[MenuViewController alloc]initWithNibName:#"MenuViewController" bundle:nil];
SegmentManagingViewController * segmentManagingViewController = [[SegmentManagingViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:segmentManagingViewController];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController,viewController ,viewController2,wvTutorial , nil];
[[UITabBar appearance]
setTintColor: [UIColor colorWithRed:120.0f/255.0f green:69.0f/255.0f blue:50.0f/255.0f alpha:1.0f]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:1.0f]];
//[[UITabBar appearance]
// setBackgroundColor: [UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:0.8f]];
navigationController.title = NSLocalizedString(#"HomePage", #"HomePage");
navigationController.tabBarItem.image = [UIImage imageNamed:#"logoSmall"];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
UIImage *navImage = [UIImage imageNamed:#"logoSmall.png"];
// self.navigationItem.setImage: navImage;
[[navigationController navigationBar] performSelectorInBackground:#selector(setBackgroundImage:) withObject:navImage];
// UIImage *navImage = [UIImage imageNamed:#"logoSmall.png"];
//[[navigationController navigationBar] performSelectorInBackground:#selector(setBackgroundImage:) withObject:navImage];
[self.window addSubview:tabBarController.view];
[segmentManagingViewController release];
//[window addSubview:self.navigationController.view];
[window makeKeyAndVisible];
return YES;
}
i have used this code working fine for me.

Related

Xcode - CoreLocation not showing blue dot

For some reason when I launch my app it does everything correctly except show the user as blue dot on the map. It zooms in to the users current location, just no dot. Any help would be greatly appreciated.
I've added code and screenshots below for resources.
FrontViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface FrontViewController : UIViewController <CLLocationManagerDelegate>
{
CLLocationManager *objLocationManager;
double latitude_UserLocation, longitude_UserLocation;
}
#property (weak, nonatomic) IBOutlet MKMapView *objMapView;
#end
FrontViewController.m
#import "FrontViewController.h"
#import "SWRevealViewController.h"
#interface FrontViewController()
// Private Methods:
- (IBAction)pushExample:(id)sender;
#end
#implementation FrontViewController
#synthesize objMapView;
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadUserLocation];
//self.title = NSLocalizedString(#"Front View", nil);
SWRevealViewController *revealController = [self revealViewController];
[revealController panGestureRecognizer];
[revealController tapGestureRecognizer];
[self.navigationController.navigationBar addGestureRecognizer:revealController.panGestureRecognizer];
UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"reveal-icon.png"]
style:UIBarButtonItemStyleBordered target:revealController action:#selector(revealToggle:)];
self.navigationItem.leftBarButtonItem = revealButtonItem;
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BSEEK-Logo.png"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIView* titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 84, 84)];
imageView.frame = titleView.bounds;
[titleView addSubview:imageView];
self.navigationItem.titleView = titleView;
}
- (IBAction)pushExample:(id)sender
{
UIViewController *stubController = [[UIViewController alloc] init];
stubController.view.backgroundColor = [UIColor whiteColor];
[self.navigationController pushViewController:stubController animated:YES];
}
- (void) loadUserLocation
{
objLocationManager = [[CLLocationManager alloc] init];
objLocationManager.delegate = self;
objLocationManager.distanceFilter = kCLDistanceFilterNone;
objLocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
if ([objLocationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[objLocationManager requestWhenInUseAuthorization];
}
[objLocationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0)
{
CLLocation *newLocation = [locations objectAtIndex:0];
latitude_UserLocation = newLocation.coordinate.latitude;
longitude_UserLocation = newLocation.coordinate.longitude;
[objLocationManager stopUpdatingLocation];
[self loadMapView];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
[objLocationManager stopUpdatingLocation];
}
- (void) loadMapView
{
CLLocationCoordinate2D objCoor2D = {.latitude = latitude_UserLocation, .longitude = longitude_UserLocation};
MKCoordinateSpan objCoorSpan = {.latitudeDelta = 0.2, .longitudeDelta = 0.2};
MKCoordinateRegion objMapRegion = {objCoor2D, objCoorSpan};
[objMapView setRegion:objMapRegion];
}
#end
Project Files
MKMapView has a showsUserLocation property. It looks like you need to turn that on.
You can set that property in code, but if your map view is created in a storyboard scene you can just check the User Location box in the storyboard editor:
Also: Make sure you have the appropriate key in your Info.plist to allow the app to access the user's location, and that you've granted the app appropriate permission on the device. (You can check in Settings->Privacy->Location Services.) If the app doesn't have permission to get the location, the map won't show it even if showsUserLocation is enabled.

Push to UIViewController from a UITabBarItem

I encounter a problem using UITabBarItem and UIButton in my application. My button is inside a UITabBarItem. When I press it I want to be pushed to another controller to display a PDF.
Here is a piece of code that works in other cases :
- (void)viewDidLoad {
UIImage* imageButton = [UIImage imageNamed:#"pdf-button.png"];
UIButton *buttonPDF = [UIButton buttonWithType:UIButtonTypeCustom];
buttonPDF.frame = CGRectMake(SCREEN_WIDTH/2 - 100, 100, 200, 36);
[buttonPDF setImage:imageButton forState:UIControlStateNormal];
buttonPDF.contentMode = UIViewContentModeScaleAspectFit;
buttonPDF.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
[buttonPDF addTarget:self action:#selector(displayPDFParams:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonPDF];
}
-(void)displayPDFParams:(UIButton *)sender {
PDFProduitController *pdfController = [[PDFProduitController alloc] init];
pdfController.pdf = documentParametres;
[self.navigationController pushViewController:pdfController animated:YES];
}
displayPDFParams is called but it not push me on my pdfController. I think it's because I'm not able to target the parent navigation controller of my application...
Anyone help would be much appreciated. Thanks in advance !
You need To initialize your root view controller with the navigation controller. Here is the code.
In your AppDelegate.h
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
#class HomeViewController;
#interface IDSAppDelegate : UIResponder <UIApplicationDelegate>{
UINavigationController *nav;
}
#property (strong, nonatomic) HomeViewController *homeViewController;
#end
In your AppDelegate.m
#import "IDSAppDelegate.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
nav=[[UINavigationController alloc]initWithRootViewController:self.homeViewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Problem solved by define a property in my UIViewController (as UITabBarItem) like this :
#property (nonatomic, retain) UINavigationController *superNavController;
And set it in my UITabBarController :
self.myViewController.superNavController = self.navigationController;
Finally I modified my displayPDFParams method :
-(void)displayPDFParams:(UIButton *)sender {
PDFProduitController *pdfController = [[PDFProduitController alloc] init];
pdfController.pdf = self.documentParametres;
[self.superNavController pushViewController:pdfController animated:YES];
}
Works perfectly !

How to make sliding menu with tabs in ios

I am using ZUUIRevealController Library.
Appdelegate.h file
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
UITabBarController *tabBarController=[[UITabBarController alloc]init];
FrontViewController *frontViewController = [[FrontViewController alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
MapViewController *frontViewController2 = [[MapViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:frontViewController2];
RevealController *revealController = [[RevealController alloc] initWithFrontViewController:navigationController rearViewController:rearViewController];
RevealController *revealController2 = [[RevealController alloc] initWithFrontViewController:navigationController2 rearViewController:rearViewController];
[revealController.tabBarItem setTitle:#"Home"];
[revealController2.tabBarItem setTitle:#"Absent note"];
revealController.tabBarItem.image=[[UIImage imageNamed:#"home_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
revealController2.tabBarItem.image=[[UIImage imageNamed:#"absent_note_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarController setViewControllers:[NSArray arrayWithObjects:revealController,revealController2,nil]];
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
}
FrontViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(#"Front View", #"FrontView");
if ([self.navigationController.parentViewController respondsToSelector:#selector(revealGesture:)] && [self.navigationController.parentViewController respondsToSelector:#selector(revealToggle:)])
{
UIPanGestureRecognizer *navigationBarPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.navigationController.parentViewController action:#selector(revealGesture:)];
[self.navigationController.navigationBar addGestureRecognizer:navigationBarPanGestureRecognizer];
[self.view addGestureRecognizer:navigationBarPanGestureRecognizer];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Slide", #"Slide") style:UIBarButtonItemStylePlain target:self.navigationController.parentViewController action:#selector(revealToggle:)];
}
}
RearViewController is a Table ViewController
MapViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(#"Map View", #"MapView");
if ([self.navigationController.parentViewController respondsToSelector:#selector(revealGesture:)] && [self.navigationController.parentViewController respondsToSelector:#selector(revealToggle:)])
{
UIPanGestureRecognizer *navigationBarPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.navigationController.parentViewController action:#selector(revealGesture:)];
[self.navigationController.navigationBar addGestureRecognizer:navigationBarPanGestureRecognizer];
[self.view addGestureRecognizer:navigationBarPanGestureRecognizer];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Slide", #"Slide") style:UIBarButtonItemStylePlain target:self.navigationController.parentViewController action:#selector(revealToggle:)];
}
}
Tabs working Properly, but the sliding menu not working properly.
First I am click the Home tab and Slide menu to view the slide Viewcontroller.Next I clicked the Second tab and sliding menu to view the slide Viewcontroller. Again I've clicked the Home tab and sliding menu to view the Slide Viewcontroller. It cannot displayed only display a black screen.
I found the solution for that problem.
in Appdelegate file i have written the swreavealtoggle call for navigation.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
HomeViewController *frontViewController = [[HomeViewController alloc] init];
SlideViewController *rearViewController = [[SlideViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
revealController.delegate = self;
self.viewController = revealController;
[self.window setRootViewController:self.viewController];
[self customizeInterface];
[self.window makeKeyAndVisible];
return YES;}
Then create a viewController to add a Tabbar item components.
Myviewcontroller name is HomeViewController.h
after i include the tab item button click action
#import <UIKit/UIKit.h>
#interface HomeViewController : UIViewController<UITabBarDelegate>{
UITabBar *mainTabBar;
UIViewController *tab1vc; // view controller of first tab
UIViewController *tab2vc; // view controller of second tab
UIViewController *tab3vc; // view controller of first tab
UIViewController *tab4vc; // view controller of second tab
}
#property (nonatomic, retain) IBOutlet UITabBar *mainTabBar;
#property (nonatomic, retain) UIViewController *tab1vc;
#property (nonatomic, retain) UIViewController *tab2vc;
#property (nonatomic, retain) UIViewController *tab3vc;
#property (nonatomic, retain) UIViewController *tab4vc;
#end
HomeViewController.m
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
// item is the selected tab bar item
switch (item.tag) {
case 1:
if (tab1vc == nil) {
self.tab1vc =[[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
}
[self.view insertSubview:tab1vc.view belowSubview:mainTabBar];
self.title = #"Home";
label.text = self.title;
//[tab1vc release];
NSLog(#"1st");
break;
case 2:
if (tab2vc == nil) {
self.tab2vc =[[Tab2 alloc] initWithNibName:#"Tab2" bundle:nil];
}
[self.view insertSubview:tab2vc.view belowSubview:mainTabBar];
//[tab2vc release];
self.title = #"Tab2";
label.text = self.title;
NSLog(#"2st");
break;
case 3:
if (tab3vc == nil) {
self.tab3vc =[[Tab3 alloc] initWithNibName:#"Tab3" bundle:nil];
}
[self.view insertSubview:tab3vc.view belowSubview:mainTabBar];
//[tab2vc release];
self.title = #"Tab3";
label.text = self.title;
NSLog(#"3rd");
break;
case 4:
if (tab4vc == nil) {
self.tab4vc =[[Tab4 alloc] initWithNibName:#"Tab4" bundle:nil];
}
[self.view insertSubview:tab4vc.view belowSubview:mainTabBar];
//[tab2vc release];
self.title = #"Tab4";
label.text = self.title;
NSLog(#"4th");
break;
default:
break;
}
}
Now its working good

Add Subview to ViewController with Block and ARC in encapsulated Class

i'am searching for a way to add a Subview (in my example a UIPickerView) to a ViewController like MBProgressHUD. My Subview is an UIView which has a UIPickerView and a UIButton on it to select an item.
I use this view in different ViewControllers, so it would be nice to encapsulate it in a own Class. My Problem is that it only works without ARC. With ARC the App crash with EXC_BAD_ACCESS CODE = 1.... So i think i have to set the variables to strong. Can anyone help me?!
The .h File looks as the following:
#
import <Foundation/Foundation.h>
typedef void(^completition)(NSString *result);
#interface CDPickerView : NSObject<UIPickerViewDataSource, UIPickerViewDelegate>
#property(nonatomic, strong) __block UIView *_view;
#property(nonatomic, strong) __block NSMutableArray *_selection;
-(void)addPickerToView:(UIView*)view withSelection:(NSMutableArray*)selection andReturnSelectedUsingBlock:(completition) compBlock;
#end
The .m File:
#import "CDPickerView.h"
#interface CDPickerView (){
// __strong UIView *_view;
// __strong NSMutableArray *_selection;
__strong void (^completitionTest)(NSString* result);
}
#end
#implementation CDPickerView
-(id)init{
if(!self){
self = [self init];
}
return self;
}
-(void)addPickerToView:(UIView *)view withSelection:(NSMutableArray *)selection andReturnSelectedUsingBlock:(completition) compblock{
self._view = view;
self._selection = [[NSMutableArray alloc] initWithArray:selection];
completitionTest = compblock;
dispatch_async(dispatch_get_main_queue(), ^(void){
[self addSelectionView];
});
}
-(void)addPickerToView:(UIView *)view withSelection:(NSMutableArray *)selection{
__view = view;
__selection = [[NSMutableArray alloc] initWithArray:selection];
dispatch_async(dispatch_get_main_queue(), ^(void){
[self addSelectionView];
});
}
-(void)addSelectionView{
__strong UIView *custView = [[UIView alloc]initWithFrame:CGRectMake(5, 5, 310, 300)];
custView.layer.cornerRadius = 10.0;
custView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.90];
__strong UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(5, 5, 290, 200)];
picker.dataSource = self;
picker.delegate = self;
[custView addSubview:picker];
__strong UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 210, 310, 40)];
[button setTitle:#"Auswählen" forState:UIControlStateNormal];
// button.frame =
button.backgroundColor = [UIColor whiteColor];
button.titleLabel.font = [UIFont systemFontOfSize:20.0];
button.titleLabel.textColor = [UIColor blackColor];
[button addTarget:self action:#selector(choiceButtonTapped) forControlEvents:UIControlEventTouchUpInside];
[custView addSubview:button];
[__view addSubview:custView];
[picker reloadAllComponents];
}
-(void)choiceButtonTapped{
UIView *view = [[__view subviews] lastObject];
UIPickerView *picker = [[view subviews] objectAtIndex:0];
NSString *result = [__selection objectAtIndex:[picker selectedRowInComponent:0]];
completitionTest(result);
[view removeFromSuperview];
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [__selection objectAtIndex:row];
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return __selection.count;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
#end
To add this view i do the following:
__strong NSMutableArray *selection = [[NSMutableArray alloc]initWithObjects:#"Test1",#"Test2",#"Test3",#"Test4", nil];
__strong CDPickerView *picker = [[CDPickerView alloc]init];
[picker addPickerToView:self.view withSelection:selection andReturnSelectedUsingBlock:^(NSString *result) {
NSLog(#"Test: %#",result);
}];
Thank for your help!
First of all it seems that you should read where __strong and __block should be user.
Change
#property(nonatomic, strong) __block UIView *_view;
#property(nonatomic, strong) __block NSMutableArray *_selection;
to
#property(nonatomic, strong) UIView *_view;
#property(nonatomic, strong) NSMutableArray *_selection;
and
completitionTest = compblock;
to
completitionTest = [compblock copy];
in -(void)addPickerToView:(UIView *)view withSelection:(NSMutableArray *)selection

-[UIPopoverController dealloc] reached while popover is still visible

I have a class (ViewOpenAppointments) where I create and display a UIPopover. This is the code to define the popover in my .h file:
#interface ViewOpenAppointments : UIView {
}
#property (nonatomic, retain) UIPopoverController *popoverController;
-(void)createOpenAppointmentsPopover: (UIButton *) obViewOpenAppts;
#end
I have a check in the code that if the popover is visible, dismiss it. This is the code:
// create popover
UIViewController* popoverContent = [[UIViewController alloc] init];
// UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 650, 416)];
ViewOpenAppointments *popoverView = [[ViewOpenAppointments alloc] initWithFrame:CGRectMake(0, 0, 650, 416)];
popoverView.backgroundColor = [UIColor whiteColor];
popoverContent.preferredContentSize = CGSizeMake(650.0, 416.0);
// create the popover controller
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate = (id)self;
[popoverController setPopoverContentSize:CGSizeMake(650, 416) animated:NO];
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
}
[popoverController presentPopoverFromRect:CGRectMake(650, 416, 10, 50) inView: obViewOpenAppts
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
The problem is that the code to dismiss the popover is never hit, which means it's not visible. But I still get the error message (described in the question title).
What am I doing wrong?
Here is a complete popover management example:
#interface ViewController () <UIPopoverControllerDelegate>
#property (nonatomic, strong) UIPopoverController* currentPop;
#end
#implementation ViewController
-(IBAction)doPopover1:(id)sender {
Popover1View1* vc = [[Popover1View1 alloc] initWithNibName:#"Popover1View1" bundle:nil];
UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:vc];
self.currentPop = pop;
[pop presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
pop.passthroughViews = nil;
// make ourselves delegate so we learn when popover is dismissed
pop.delegate = self;
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)pc {
self.currentPop = nil;
}
By checking self.currentPop you can make sure you don't present two popovers at once (illegal anyway).