UIBarButtonItem doesn't appear - ios

I have an issue, when the view is loaded, it will load the UIBarButtonItem, but then i have to set it to nil in "mostra_filtro_btn", but then again in "load_map" i have to set it, but it doesn't show up.
Here is my code:
//
// FirstViewController.m
// House Finder
//
// Created by Giovanni Poli on 12/05/15.
// Copyright (c) 2015 Giovanni Poli. All rights reserved.
//
#import "MapViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "Reachability.h"
#import "UIKit/UIKit.h"
#import <Foundation/Foundation.h>
#import "FiltroViewController.h"
#interface MapViewController ()
#end
#implementation MapViewController
#synthesize filtro_controller,overlay_filtro_counter,mappa_controller;
-(void)load_map{
NSLog(#"load_map");
[mapView removeFromSuperview];
[mappa_controller.view removeFromSuperview];
[filtro_controller.view removeFromSuperview];
UIBarButtonItem *Button = [[UIBarButtonItem alloc] initWithTitle:#"Filtro" style:UIBarButtonItemStyleBordered target:self action:#selector(mostra_filtro_btn:)];
[Button setTitle:#"Filtro"];
[Button setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = Button;
}
- (id) init{
filtro_controller = [[FiltroViewController alloc]init];
[self.view addSubview:filtro_controller.view];
[self addChildViewController:filtro_controller];
return self;
}
- (void)viewDidLoad {
self.title = #"Mappa";
self.navigationItem.title = #"House Finder";
self.navigationController.navigationBar.translucent = FALSE;
UIBarButtonItem *Button = [[UIBarButtonItem alloc] initWithTitle:#"Filtro" style:UIBarButtonItemStyleBordered target:self action:#selector(mostra_filtro_btn:)];
[Button setTitle:#"Filtro"];
[Button setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = Button;
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"json"];
NSData *content = [[NSData alloc] initWithContentsOfFile:filePath];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:content options:kNilOptions error:nil];
NSArray * json_all = [json objectForKey:#"results"];
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-40)];
mapView.delegate = self;
mapView.showsUserLocation = NO;
mapView.userInteractionEnabled = YES;
CLLocationCoordinate2D annotationCoord;
self.view.userInteractionEnabled = YES;
NSDictionary * temp;
for (id object in json_all) {
temp = object[#"titolo"];
annotationCoord.latitude = [object[#"lat"] floatValue];
annotationCoord.longitude = [object[#"lon"] floatValue];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = object[#"titolo"];
annotationPoint.subtitle = object[#"agenzia"];
[mapView addAnnotation:annotationPoint];
}
[mapView showAnnotations:[mapView annotations] animated:YES];
[self.view addSubview:mapView];
[self.view setNeedsDisplay];
}
- (IBAction) mostra_filtro_btn: (id)sender{
NSLog(#"mostra_filtro_btn");
self.navigationItem.rightBarButtonItem = nil;
filtro_controller = [[FiltroViewController alloc]init];
[self.view addSubview:filtro_controller.view];
[self addChildViewController:filtro_controller];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:annotation{
if (annotation == mapViews.userLocation) return nil;
MKPointAnnotation * temp = annotation;
MKAnnotationView * m = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"casa"];
m.canShowCallout = YES;
m.enabled = YES;
m.userInteractionEnabled = YES;
NSString * icon_file = [NSString stringWithFormat:#"%#.png",temp.subtitle];
m.image = [UIImage imageNamed:icon_file];
return m;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
NSLog(#"overlay prezzo");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end

Use the following code to nil the navigation item so it will hide where ever you want
[self.navItem setRightBarButtonItem:nil];
[self.navItem setLeftBarButtonItem:nil];

Try first create the UIButton then put it into the navigation button as follow
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 44.0f, 30.0f)];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popVC) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

Since I can't post this as a comment here is what I think is missing inside load_map:
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"load_map");
[mapView removeFromSuperview];
[mappa_controller.view removeFromSuperview];
[filtro_controller.view removeFromSuperview];
UIBarButtonItem *Button = [[UIBarButtonItem alloc] initWithTitle:#"Filtro" style:UIBarButtonItemStyleBordered target:self action:#selector(mostra_filtro_btn:)];
[Button setTitle:#"Filtro"];
[Button setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = Button;
})
This is a possible solution till you try it. If you share the code from where you call load_map it would be great!
Cheers!

Your code works perfect, as you said you are adding button with method mostra_filtro_btn : in viewDidLoad method, and on click of button, button from navigation bar set to nil.
But the problem is that, from nowhere you calls load_map method. You need to call this method once the button gets nil, to get it back with load_map method.
May you get it.
Enjoy Coding!!

Related

Parent child navigation UIViewController causes exception for editing form

I have a parent->child navigation setup in application. I use navigation via pushViewController function.
-(void)loadMemosViewController:(id)sender{
if(activeHullGuid != nil && activeHullGuid.length > 0)
{
NSString *storyboardName = #"MainStoryboard_iPhone1";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MemosViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:#"sid_Memos"];
loginVC.keyReference = [[KeyValuePairIS alloc] initWithData:&controllerID:activeHullGuid];
[self.navigationController pushViewController:loginVC animated:YES];
}
}
for back navigation I use only default implementation in IOS (that would be a click on a back button).
This setup works for most situations, but recent implementation is causing problems.
The problem is this:
I have parent view controller named "hullViewController" and a child "memosViewController". The navigation between them works. Child does not report any information back to parent. HullViewController is also an editable form, which changes edit state via button in navigation bar.
Now if I change this edit/read state on hullViewController works nonstop. If I visit the child memosViewController, and go back to parent, I can only change state once more, then application crashes with exc_bad_access code=1.
After profiling with "Zombies" I found the culprit for exception is my probably disposed child memosViewController.
An Objective-C message was sent to a deallocated 'MemosViewController' object (zombie) at address: 0xdd52f10
it seams to crash on an IOS internal event, since none of my breakpoints are hit before crash.
A you can see the child is instanced during creation and I don't reference it to nothing else. Why would the edit state change request the child object?
What I tried already:
-declaring MemosViewController as a class variable. (application did not crash anymore, but would not change state anymore).
-initialising MemosViewController on viewDidLoad, changed nothing.
-calling child with class init only (not via storyboard), loaded child without UI, but result was same.
Project is set up with ARC, so I have minimum control on disposal of objects.
I have been searching for a solution quite a while now, with no results. Any help to solve my error editing if I visit the child would be appreciated.
UPDATE
I have additionally discovered, that when I get back to parent from child, the reference self.navigationItem still points to child, and any update to navigation buttons crashes the app.
**attaching custom ViewController, since it could be related to problem **
#import "UITableViewControllerEx.h"
#import "UITextFieldEx.h"
#import "UITextViewEx.h"
#import "GlobalValues.h"
#import "UITableViewEx.h"
#interface UITableViewControllerEx ()
#end
#implementation UITableViewControllerEx
UIBarButtonItem *bbi_navigateToMaster;
UIBarButtonItem *editButton;
UIButton *cmdEdit;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self setNavigationBackground];
[self setApplicationTintColor];
[self setApplicationTitleFont];
[self setupLeftBarButtonItem];
[self setBackButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//UITextFieldEx delegate to control the length of fields
- (BOOL)textField:(UITextFieldEx *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > textField.maxLength) ? NO : YES;
}
//UITextViewEx delegate to control the length of fields
-(BOOL)textView:(UITextViewEx *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSUInteger newLength = [textView.text length] + [text length] - range.length;
return (newLength > textView.maxLength) ? NO : YES;
}
//function to set left button to always pop to root controller
- (void)setBackButtonToReturnToMaster {
UIButton *cmdHome = [[UIButton alloc] initWithFrame:CGRectMake(0,0,30,30)];
[cmdHome setImage:[UIImage imageNamed:#"home"] forState:UIControlStateNormal];
bbi_navigateToMaster = [[UIBarButtonItem alloc] initWithCustomView:cmdHome];
[cmdHome addTarget:self action:#selector(backToMaster:) forControlEvents:UIControlEventTouchUpInside ];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:bbi_navigateToMaster , nil];
/*
bbi_navigateToMaster = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:self action:#selector(backToMaster:)];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:bbi_navigateToMaster , nil];
[bbi_navigateToMaster setImage:[UIImage imageNamed:#"home"]];
[bbi_navigateToMaster setImageInsets:UIEdgeInsetsMake(2, 2, 2, 2)];*/
}
//pop to root controller
-(void)backToMaster:(id)sender {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
}
else { [self.navigationController popToRootViewControllerAnimated:YES]; }
}
//find superview element of given type
- (UIView *)findSuperViewWithClass:(Class)superViewClass uiViewToSearch:(UIView*)bottomView{
UIView *superView = bottomView.superview;
UIView *foundSuperView = nil;
while (nil != superView && nil == foundSuperView) {
if ([superView isKindOfClass:superViewClass]) {
foundSuperView = superView;
break;
} else {
superView = superView.superview;
}
}
return foundSuperView;
}
-(void)setNavigationBackground{
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:#"navigationBackground"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
UIView* uv = [[UIView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height-1,self.navigationController.navigationBar.frame.size.width, 1)];
[uv setBackgroundColor:[GlobalValues getTintColor]];
[self.navigationController.navigationBar insertSubview:uv atIndex:10];
}
}
//sets the tint color of szstem items (title, szstem buttons, ...)
-(void)setApplicationTintColor {
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
self.navigationController.navigationBar.barTintColor = [GlobalValues getTintColor];
self.navigationController.navigationBar.tintColor = [GlobalValues getTintColor];
self.navigationController.navigationBar.translucent = NO;
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
UIColor *color = [GlobalValues getTintColor];
self.view.tintColor = color;
}else {
//self.navigationController.navigationBar.tintColor = [GlobalValues getTintColor];
/*NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];*/
}
}
//sets the navigation title
-(void)setApplicationTitleFont {
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
[self.navigationController.navigationBar setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"HelveticaNeue-Light" size:21],
NSFontAttributeName, [UIColor whiteColor], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil]];
}else {
[self.navigationController.navigationBar setTitleTextAttributes: #{
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeFont: [UIFont fontWithName:#"Helvetica-Light" size:21.0f]
}];
}
}
-(void)setupLeftBarButtonItem{
cmdEdit = [[UIButton alloc] initWithFrame:CGRectMake(0,0,30,30)];
[cmdEdit setImage:[UIImage imageNamed:#"locked"] forState:UIControlStateNormal];
editButton = [[UIBarButtonItem alloc] initWithCustomView:cmdEdit];
[cmdEdit addTarget:self action:#selector(setEditState) forControlEvents:UIControlEventTouchUpInside];
}
- (UIBarButtonItem *)leftBarButtonItem
{
if (self.tableView.editing) {
[cmdEdit setImage:[UIImage imageNamed:#"unlocked"] forState:UIControlStateNormal];
return editButton;
}
else {
[cmdEdit setImage:[UIImage imageNamed:#"locked"] forState:UIControlStateNormal];
return editButton;
}
}
-(void)updateEditButton{
if (self.tableView.editing) {
[cmdEdit setImage:[UIImage imageNamed:#"unlocked"] forState:UIControlStateNormal];
}
else {
[cmdEdit setImage:[UIImage imageNamed:#"locked"] forState:UIControlStateNormal];
}
}
-(void)setEditState{
if (!self.tableView.editing) {
[self setEditing:YES animated:YES];
} else {
[self setEditing:NO animated:YES];
}
[self updateEditButton];
}
}*/
-(void) setBackButton{
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"back"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 30, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
}
- (void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Table view data source
#pragma mark - Table view delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundView=[[UIView alloc] initWithFrame:CGRectZero];
cell.backgroundColor = [UIColor clearColor];
cell.layer.backgroundColor = [UIColor clearColor].CGColor;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
UIView *customTitleLineView = [ [UIView alloc] initWithFrame:CGRectMake(10, 43, self.view.frame.size.width -20, 0.5f)];
customTitleLineView.backgroundColor = [GlobalValues getTintColor];
UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(20, 0, 300, 44)];
titleLabel.text = [self tableView:tableView titleForHeaderInSection:section];
titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:18];
titleLabel.textColor = [GlobalValues getTintColor];
titleLabel.backgroundColor = [UIColor clearColor];
if (titleLabel.text.length != 0) {
[customTitleView addSubview:customTitleLineView];
}
[customTitleView addSubview:titleLabel];
return customTitleView;
}
#end
Seems I have found a solution to my problem.
Class UITableViewControllerEx contains functionality to setup edit button. The class variable "UIBarButtonItem *editButton;" is then used as edit button on all forms that inherit from "UITableViewControllerEx"
the solution was to instantiate UIBarButtonItem on each form inheriting UITableViewControllerEx with local name (like editButtonHull) and given as param to logic of superclass.
Thanks to #akashg for suggestion that Navigation bar modification might be the problem

ViewController goes black when pushing it in UINavigationcontroller stack

I have a UInavigationController stack. In the RootViewController i have button . When i click the button a new viewcontroller is pushed and UIDatePicker should be shown . the problem is when navigating to view controller the date picker is shown for 1 sec and then suddenly disappeared and the whole screen goes Black. Here is the code
//rootviewcontroller
- (IBAction)dateAction:(id)sender
{
self.another = [[AnotherViewController alloc]initWithButtonWithTag:self.dateButton.tag andDelegate:self];
[self.navigationController pushViewController:self.another animated:YES];
self.another.navigationItem.title = #"DateName";
}
//second viewcontroller
- (void)viewDidload
{
self.pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
[self.pickerView addTarget:self action:#selector(datePickerDidChangeDate:) forControlEvents:UIControlEventValueChanged];
self.pickerView.datePickerMode = UIDatePickerModeDate;
self.pickerView.hidden = NO;
self.pickerView.date = [NSDate date];
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateStyle = NSDateFormatterLongStyle;
[self.view addSubview:self.pickerView];
}
A help would be appreciated
try this codeā€¦
first view controller
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *temp=[NSArray arrayWithObjects:#"button1" ,#"button2" ,#"button3",nil];
for (int i=0; i<=2; i++)
{
_likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[_likeBtn setFrame:CGRectMake(20+(i*80), 220, 102, 20 )];
[_likeBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_likeBtn setTitle:[temp objectAtIndex:i] forState:UIControlStateNormal];
[_likeBtn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
[_likeBtn setTag:i];
[self.view addSubview:_likeBtn];
}
// Do any additional setup after loading the view from its nib.
}
- (IBAction)action:(id)sender
{
UIButton *button=(UIButton *)sender;
NSLog(#"%d",button.tag);
secViewController *obj = [[secViewController alloc]init];
obj.tagValue=button.tag;
[self.navigationController pushViewController:obj animated:YES];
obj.navigationItem.title = #"DateName";
}
second view .h file
#property(nonatomic,assign) NSInteger *tagValue;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger *tag=self.tagValue;
if (tag==0)
{
//tableView
NSLog(#"tableview");
}
else
{
//datepicker
NSLog(#"picker");
self.pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
[self.pickerView addTarget:self action:#selector(datePickerDidChangeDate:) forControlEvents:UIControlEventValueChanged];
self.pickerView.datePickerMode = UIDatePickerModeDate;
self.pickerView.hidden = NO;
self.pickerView.date = [NSDate date];
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateStyle = NSDateFormatterLongStyle;
[self.view addSubview:self.pickerView];
}
// Do any additional setup after loading the view from its nib.
}
-(void)datePickerDidChangeDate:(id)sender
{
NSLog(#"%#",self.pickerView.date);
}
let me know statisfied or not...

NSURLErrorDomain error -999 IOS7

I am experimenting this issue while trying to open a webpage in an ios app. Before ios7 it was working perfect but now we are upgrading our app and this is the only serious problem we have.
About this problem I found that it is produced when a call is made twice. And I think this is what is happening here because if I write a NSLog text in didFailLoadWithError, it shows twice.
I tried ignoring the error but the loading circle is shown ethernally and webpage not loads.
I paste here my code, maybe you can help us or give us a clue.
#import "WebPoiViewController.h"
#import "FavoriteManager.h"
#interface WebPoiViewController ()
#end
#implementation WebPoiViewController
#synthesize webView,urlStr, activityIndicator,title2,poi=_poi,position=_position;
- (void)viewDidLoad { //We have a NIB file in play here, so I dropped the loadView here. Just make sure that your loadView is not getting called twice!
[super viewDidLoad];
if (self.poi!=nil){
self.position = [[FavoriteManager sharedInstance] isPoiInFavoriteList:self.poi];
if (self.position==-1){
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,30,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"Favorites_icon_inactive.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(makeFavorite:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
//self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"Favorites_icon_inactive.png"] style: UIBarButtonItemStylePlain target:self action:#selector(makeFavorite:)];
} else {
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,30,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"Favorites_icon.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(deleteFavorite:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
//self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"Favorites_icon.png"] style: UIBarButtonItemStylePlain target:self action:#selector(deleteFavorite:)];
}
}
if (nil!=title2&& ![title2 isEqualToString:#""]){
self.title=self.title2;
}
[self loadView];
}
- (IBAction)makeFavorite:(id)sender {
self.position=[[FavoriteManager sharedInstance] addFavotitePOI:self.poi];
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,30,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"Favorites_icon.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(deleteFavorite:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
}
- (IBAction)deleteFavorite:(id)sender {
[[FavoriteManager sharedInstance] deleteFavotitePOIat:self.position];
self.position=-1;
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,30,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"Favorites_icon_inactive.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(makeFavorite:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
}
- (void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = contentView;
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y = 0.0f;
webView = [[UIWebView alloc] initWithFrame:webFrame];
webView.backgroundColor = [UIColor blueColor];
webView.scalesPageToFit = YES;
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
webView.delegate = self;
if([webView respondsToSelector:#selector(scrollView)]){
webView.scrollView.bounces = NO;
}
[self.view addSubview: webView];
if (nil!=self.urlStr && ![urlStr isEqualToString:#""]){
NSLog(#"NO ES NIL");
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
//activityIndicator.center = self.view.center;
activityIndicator.center = CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2);
[self.view addSubview: activityIndicator];
} else{
[webView loadHTMLString:#"<html><center><br /><br /><font size=+5>No info<br /><br /></font></center></html>" baseURL:nil];
}
}
#pragma mark WEBVIEW Methods
- (void)webViewDidStartLoad:(UIWebView *)myWebView
{
// starting the load, show the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)myWebView
{
// finished loading, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[activityIndicator stopAnimating];
}
- (void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error
{
if([error code] == NSURLErrorCancelled) return; // Ignore this error
NSLog(#"FAIL");
// load error, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// report the error inside the webview
NSString* errorString = [NSString stringWithFormat:
#"<html><center><br /><br /><font size=+5 color='red'>Error<br /><br />Your request %#</font></center></html>",
error.localizedDescription];
[webView loadHTMLString:errorString baseURL:nil];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
- (BOOL)shouldAutorotate {
return [self.navigationController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
return [self.navigationController supportedInterfaceOrientations];
}
#end
I solved it by myself. I want to share the answer because maybe it helps others with the same problem.
I just added in didFailWithError this code:
- (void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error
{
if([error code] != NSURLErrorCancelled) // only goes in if it is not -999
{
// load error, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// report the error inside the webview
NSString* errorString = [NSString stringWithFormat:
#"<html><center><br /><br /><font size=+5 color='red'>Error<br /><br />Your request %#</font></center></html>",
error.localizedDescription];
[webView loadHTMLString:errorString baseURL:nil];
}
else{
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] ]];
}
}
Now, if it fail, it tries to repeat the load until it loads. It can be improved by using a counter. Just trying it 5 or 10 times and not more like mine.

ZBar remove info button (ios 6)

How to I remove the information button from the toolbar at the bottom screen (reader screen).I have tried the using the overlay method but it didn't work. Hope someone would save me. Thanks
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
ZBarReaderController *reader = [ZBarReaderController new];
reader.readerDelegate =self;
reader.sourceType =UIImagePickerControllerSourceTypeCamera;
reader.showsCameraControls=YES;
// Define button title color when disabled (Grey)
[btnFindMe setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
[self activateScan];
}
-(void) activateScan
{
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
[reader.scanner setSymbology: 0
config: ZBAR_CFG_ENABLE
to: 0];
[reader.scanner setSymbology: ZBAR_QRCODE
config: ZBAR_CFG_ENABLE
to: 1];
reader.readerView.zoom = 1;
reader.showsZBarControls=FALSE;
UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,480.0)];
overlay.backgroundColor = [UIColor clearColor];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0,510.0,320.0,54.0)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:#selector(closeScanner)];
toolbar.items = [NSArray arrayWithObjects:cancelButton,nil];
[overlay addSubview:toolbar];
reader.cameraOverlayView = overlay;
[self presentModalViewController: reader animated: YES];
}
I ended up creating new UIView & apply it to cameraOverlayView property of ZBarReaderViewController's object codeReader.
It's actually recommended from ZBar official docs.
self.codeReader.showsCameraControls = NO;
self.codeReader.showsZBarControls = NO;
[self.codeReader setCameraOverlayView:self.viewCancelButtonQRCodeReader];
[Here's the screenshot of UIView that i added. It has background color as clearColor.]

how to move to the new view fast in ipad application

I have many view controller when i click on tableView cell it move to new view controller problem is that it takes alot of time to move to the next view may be due to view which is to load fetches data from server here is my code for the view which loads
- (void)viewDidLoad {
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
UIImageView *bottomImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Nav.png"]];
[bottomImageView setFrame:CGRectMake(0,690,1024,34)];
[self.view addSubview:bottomImageView];
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(10.0, 2.0, 88, 40.0)];
[button1 addTarget:self action:#selector(loginPressed) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:#"logoutN.png"] forState:UIControlStateNormal];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:button1];
self.navigationItem.rightBarButtonItem = button;
self.title=#"Catalog";
popImageView.hidden=YES;
passwordLabel.hidden=YES;
userLabel.hidden=YES;
userNameTextField.hidden=YES;
userPasswordTextField.hidden=YES;
signInButton.hidden=YES;
tableView.hidden=NO;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
if(!categoryArray){
categoryArray =[[NSMutableArray alloc] init];
}
if(!userArray){
userArray =[[NSMutableArray alloc] init];
}
if(!subCategoryArray){
subCategoryArray =[[NSMutableArray alloc] init];
}
if(!subCategoryArrayOne){
subCategoryArrayOne =[[NSMutableArray alloc] init];
}
if(!subCategoryArrayTwo){
subCategoryArrayTwo =[[NSMutableArray alloc] init];
}
[self setUpData];
[self setUpDataSub];
[self setUpDataSubOne];
[self setUpDataSubTwo];
int count=[appDelegate.coffeeArray count];
NSLog(#"Arrays Content Are %d",count);
tableView.backgroundView = nil;
[super viewDidLoad];
}
is there any way so that view loads fast
Your view is not loading fast because of I guess those data set operation in viewDidLoad method . I think those are :
[self setUpData];
[self setUpDataSub];
[self setUpDataSubOne];
[self setUpDataSubTwo];
The one thing you could do is to move this operations to perform on the background thread . For that move this operations to the separate function and call that to perform on background from the view did load method :
-(void)dataOperations
{
[self setUpData];
[self setUpDataSub];
[self setUpDataSubOne];
[self setUpDataSubTwo];
}
and in viewDidLoad call this function in background:
[self performSelectorInBackground:#selector(dataOperations) withObject:nil];
Or you can directly call those method from viewDidLoad like :
[self performSelectorInBackground:#selector(setUpData) withObject:nil];

Resources