UIView not appearing - ios

So I have this code -
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
self.searchController = [[PlacesViewController alloc]initWithNibName:#"PlacesView" bundle:nil];
NSLog(#"TEXT FIELD DID BEGIN EDITIN");
self.navigationItem.hidesBackButton = YES;
[self.navigationController pushViewController:self.searchController animated:YES];
return NO;
}
The NSLog runs, but my view doesn't appear. This is happening in a controller that has a view that exists in the navigation bar. Like this -
-(void)addSearchBar{
self.searchBarController = [[SearchBarController alloc] initWithNibName: #"SearchBar" bundle: nil];
self.navigationItem.titleView = self.searchBarController.view;
}
where addSearchBar is called in the initWithNibName method.
Why is my view not appearing?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
searchQuery = [[SPGooglePlacesAutocompleteQuery alloc] init];
searchDetailQuery = [[SPGooglePlacesPlaceDetailQuery alloc] init];
searchQuery.radius = 2000;
shouldBeginEditing = YES;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
return self;
}
I've also proven that the view works because when I do this -
-(void)textFieldDidBeginEditing:(UITextField *)textField{
self.searchController = [[PlacesViewController alloc]initWithNibName:#"PlacesView" bundle:nil];
//may have to be strong.
NSLog(#"SELF VIEW : %#", self.view);
textField.inputView.center = self.view.center;
textField.inputView.bounds = self.view.bounds;
textField.inputView = self.searchController.view;
}
The view appears in place of the keyboard.

You can move the addSearchBar from initWithNibName to viewDidLoad for a try. In fact, usually in the init method, the view doesn't initialize. The view will be lazily initialize when it is first access, usually when you show the view on the screen, and the viewDidLoad method will be call immediately after the view is initialized. So usually the init method only configure data related property, and the view related property usually configured in the viewDidLoad method.

Related

Navigation Bar's Button Item Not Clickable on iPad

In my app, I use the old GKImage library for adding pictures and being able to custom crop them easily. This works pretty well, except when it comes to iPad. On iPhone, everything works great. The main issue is this:
When I use an iPad, none of the barButtonItems are clickable.
Here is my code for this view:
- (void)_actionCancel{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)_actionUse{
NSLog(#"PRESSED");
_croppedImage = [self.imageCropView croppedImage];
[self.delegate imageCropController:self didFinishWithCroppedImage:_croppedImage];
}
- (void)_setupNavigationBar{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(_actionCancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Use"
style:UIBarButtonItemStylePlain
target:self
action:#selector(_actionUse)];
}
- (void)_setupCropView{
self.imageCropView = [[GKImageCropView alloc] initWithFrame:self.view.bounds];
[self.imageCropView setImageToCrop:sourceImage];
[self.imageCropView setResizableCropArea:self.resizeableCropArea];
[self.imageCropView setCropSize:cropSize];
[self.view addSubview:self.imageCropView];
}
#pragma mark -
#pragma Super Class Methods
- (id)init{
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
self.title = #"Choose Photo";
[self _setupNavigationBar];
[self _setupCropView];
[self.navigationController setNavigationBarHidden:NO];
}
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
self.imageCropView.frame = self.view.bounds;
}
The code used to present this controller is:
self.imagePicker = [[GKImagePicker alloc] init];
self.imagePicker.cropSize = CGSizeMake(280, 280);
self.imagePicker.delegate = self;
self.imagePicker.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePicker.imagePickerController animated:YES completion:nil];
Is it an issue with presenting the view controller?

how to fix over released objects

NSZombie detected that one of the objected is over released in my app and that is causing the app to crash every time when a button is pressed. However, after inspecting the source code of where the over release happens, I couldn't see any obvious code that that may have caused the release. Can xcode release objects automatically without any actual code?
Below are the places reported by the Instrument that has a release event:
-(void) takePicture:(CDVInvokedUrlCommand *)command {
CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
self.pickerController = cameraPicker;
CameraOverlayViewController* overlay = [[CameraOverlayViewController alloc] initWithNibName:#"CameraOverlayViewController" bundle:nil];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.cameraOverlayView = overlay.view;
[self.viewController presentViewController:cameraPicker animated:YES completion:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
Any ideas on how to fix the problem? Thanks in advance!
I think CameraOverlayViewController is released.
Try the below code
CameraOverlayViewController* overlay;
-(void) takePicture:(CDVInvokedUrlCommand *)command {
CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
self.pickerController = cameraPicker;
overlay = [[CameraOverlayViewController alloc] initWithNibName:#"CameraOverlayViewController" bundle:nil];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.cameraOverlayView = overlay.view;
[self.viewController presentViewController:cameraPicker animated:YES completion:nil];
}
You're creating a CameraOverlayViewController and keeping a reference to it's view, but the CameraOverlayViewController itself is going out of scope when the takePicture: method ends. A view doesn't retain it's view controller. You need to either keep a reference to the CameraOverlayViewController, or if you're only interested in the view object itself, just create a view from the xib file and set the cameraPicker.cameraOverlayView to that view.
EDIT:
For example, in your header file you could make a property:
#property (strong) CameraOverlayViewController *overlay;
and in your method:
-(void) takePicture:(CDVInvokedUrlCommand *)command {
CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
self.pickerController = cameraPicker;
self.overlay = [[CameraOverlayViewController alloc] initWithNibName:#"CameraOverlayViewController" bundle:nil];
cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraPicker.cameraOverlayView = self.overlay.view;
[self.viewController presentViewController:cameraPicker animated:YES completion:nil];
}
Once you're done with the camera, you'll want to let go of the reference. So for example:
-(void) cameraPickerFinished {
self.overlay = nil;
}

Custom transition between view controllers in ios6

I have a transition class that has two views in the same view controller. This works ok. I want to apply the same transition to a second view controller. I'm not getting any exceptions, but it isn't working...
The current transition code is like this:
Custom Transition class
- (id)initWithBaseView:(UIView *)baseView firstView:(UIView *)firstView lastView:(UIView *)lastView
{
if((self = [super init])) {
self.view = baseView;
self.originalView = firstView;
self.nextView = lastView;
}
return self;
}
This is the code that is working:
Single View Controller class
- (IBAction)doTransition:(id)sender
{
MyTransition *transition = [[MyTransition alloc] initWithBaseView:self.view
firstView:self.currentView
lastView:self.nextView];
[transition buildAnimation];
}
I would like to achieve something like this:
- (IBAction)doTransition:(id)sender
{
NSLog(#"%s", __FUNCTION__);
MyTransition *transition = [[MyTransition alloc] initWithBaseView:self.view
firstView:self.currentView
lastView:secondView.lastView];
[transition buildAnimation];
}
- (void)viewDidLoad
{
NSLog(#"%s", __FUNCTION__);
[super viewDidLoad];
secondView = [[SecondViewController *) secondView initWithNibName:#"SecondViewController" bundle:nil];
}
where First View is in the firstView Controller and next view is in the second view controller..
UPDATE:
I've updated the First VC as follows:
- (void)viewDidLoad
{
NSLog(#"%s", __FUNCTION__);
[super viewDidLoad];
secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
}
- (IBAction)doTransition:(id)sender
{
NSLog(#"%s", __FUNCTION__);
MyTransition *transition = [[MyTransition alloc] initWithBaseView:self.view firstView:self.currentView lastView:secondView.view];
[transition buildAnimation];
}
Logging the second VC shows that it is not called..
I think it should work if you try something like this:
- (IBAction)doTransition:(id)sender
{
NSLog(#"%s", __FUNCTION__);
MyTransition *transition = [[MyTransition alloc] initWithBaseView:self.view
firstView:self.currentView
lastView:secondView.view];
[transition buildAnimation];
}
- (void)viewDidLoad
{
NSLog(#"%s", __FUNCTION__);
[super viewDidLoad];
secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
}
If i understand what you're trying to do, then you should be able to perform the same animation on the SecondViewController's view, just like you would with a regular view. Or am I missing something else?
The problem turned out to be that I was not initializing the secondView correctly in its own ViewController. Once I did that, the transition worked as needed.
For others, the code I used was as follows:
FirstViewController:
- (void)viewDidLoad
{
NSLog(#"%s", __FUNCTION__);
[super viewDidLoad];
secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
}
- (IBAction)clickOpenDoor:(id)sender
{
MyTransition *transition = [[MyTransition alloc] initWithBaseView:self.view
firstView:self.currentView lastView:secondView.view];
[transition buildAnimation];
}
Second View Controller:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(#"%s", __FUNCTION__);
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350, 500)];
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 350, 500)];
[view addSubview:tableView];
}

Loading data in one tab, viewing it another but the table won't reload

I have a tab controller with 4 tabs in it. In the 2nd tab I am entering data and saving that data to a plist. In the 4th tab I am reading the plist back, making an array of one of the dictionary items (company names) and displaying them in a table. I am doing this in the viewDidLoad method.
This works fine for existing data. However, when I add data in the 2nd tab and then go to the 4th tab I can't figure out how to get the table to reload the data...I've tried [table reloadData] in the viewDidAppear method, no luck. Also tried to have a button to reload the data but nothing.
Could someone point me in the right direction on this?
Here's some code:
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//We need to implement the view controllers within the tab controller and make the tab controller the root controller of our app - note we are only using view 1-4 at first.
FirstViewController *fistView = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
ThirdViewController *thirdView = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
FourthViewController *fourthView = [[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil];
NSArray *viewControllersArray = [[NSArray alloc] initWithObjects:fistView, secondView, thirdView, fourthView, nil];
self.tabController = [[UITabBarController alloc] init];
[self.tabController setViewControllers:viewControllersArray animated:YES];
self.window.rootViewController = self.tabController;
//end custom code
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
in my FourthViewController.h:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//add our own image to the tab bar
self.title = #"Results";
self.tabBarItem.image = [UIImage imageNamed:#"btnResults.png"];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
newFileManager = [FileManager new];
NSMutableDictionary* surveyResults = [newFileManager readPlist];
NSMutableArray* tempArray = [[NSMutableArray alloc] init];
for(NSString* thisCompanyName in surveyResults) {
[tempArray addObject:thisCompanyName];
}
companyNames = [[NSArray alloc] initWithArray:tempArray];
NSArray* sortedCompanyNames = [companyNames sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
self.companyNamesTable = sortedCompanyNames;
}
Need to post more code to be sure but I would say you are not initializing the 4th tab everytime. Meaning you code is something like this.
init{
[tab4 init]
}
callview{
[pushView tab4]
}
where as if you did
init{
}
callview{
[tab4 init]
[pushView tab4]
}
(meaning dont init the 4tab till your going to use it)
it should query the plist fresh, without any prior query results in the way

Programmatically change NIB checkboxes

I'm trying to set my nib properties programmatically. Specifically, I have a view controller which I initialized with a nib, and now I'm trying to programmatically [mapView setMapType:MKMapTypeHybrid] but it never sets it.
My mapView is an IBOutlet MKMapView, and I dragged a Map View into my nib and conected mapView to Map View.
If I set the Type in the Attributes Inspector of the Map View, it works fine. Is there a way to do this programmatically?
I gave up trying to use nibs months ago (shortly after I started with XCode), but it would be really nice to figure this out.
Thanks
MapTabViewController.h
#import <MapKit/MapKit.h>
#interface MapTabViewController : UIViewController <MKMapViewDelegate>
#property (nonatomic, strong) IBOutlet MKMapView *mapView;
MapTabViewController.m
-(id) initWithTabBarAndNibName: (NSString *) nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = MAPTAB_TITLE;
self.tabBarItem.image = [UIImage imageNamed:MAPTAB_ICON];
self.mapView = [[MKMapView alloc] init];
[self.mapView setMapType:MKMapTypeHybrid];
}
return self;
}
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.dataModel = [[DataModel alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
tabBarController = [[UITabBarController alloc] init];
vc_tacTab = [[TacTabViewController alloc] initWithTabBarAndNibName:#"TacTabViewController" bundle: nil];
vc_tacTab.dataModel = self.dataModel;
vc_mapTab = [[MapTabViewController alloc] initWithTabBarAndNibName:#"MapTabViewController" bundle:nil];
vc_mapTab.dataModel = self.dataModel;
NSArray *localControllersArray = [[NSArray alloc] initWithObjects:vc_tacTab, vc_mapTab, nil];
tabBarController.viewControllers = localControllersArray;
[self.window addSubview:tabBarController.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
self.mapView = [[MKMapView alloc] init];
Here you are destroying the value in your outlet, and replacing it with a new mapview, which will never be displayed.
When loading from the nib, the outlets will be populated with the objects you have linked them to in the nib. Remove this line and you should be fine.
EDIT - Just realised where this code is being executed. The outlet won't be populated yet - you need to set the property in viewDidLoad, not in the overridden initializer - in fact, all of that code would be better placed in viewDidLoad.

Resources