I am trying to send a nsmutablearray from one navigational embedded view controller to another viewcontroller after this method
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{NSLog(#"tapped %# %d",view.annotation.title,[res count]);
gpDetailViewController *gpDetailView = [[gpDetailViewController alloc] init];
gpDetailView.title = #"Details";
gpDetailView.detailArray = res;
[self.navigationController pushViewController:gpDetailView animated:YES];
}
is executed.
Here is the interface and implementation files for gpDetailViewController.
//
// gpDetailViewController.h
// livewell prototype
//
// Created by MyOxygen Mobile on 02/05/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface gpDetailViewController : UIViewController{
NSMutableArray *detailArray;
}
#property (nonatomic,retain) NSMutableArray *detailArray;
#end
and gpDetailViewController.m
//
// gpDetailViewController.m
// livewell prototype
//
// Created by MyOxygen Mobile on 02/05/2012.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "gpDetailViewController.h"
#interface gpDetailViewController ()
#end
#implementation gpDetailViewController
#synthesize detailArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
detailArray = [[NSMutableArray alloc]init];
NSLog(#"%#",[self.detailArray count]);
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I am getting a null value when i am trying to print the array elements size.
What could be the reason?
Instead of
NSLog(#"%#",[self.detailArray count]);
try this
NSLog(#"%d",[self.detailArray count]);
count is an integer, not a class
Related
Suppose, I've two view controller. ViewController1 is the sub class of ViewController2. From subclass (ViewController1), I called superclass init method & passed a value by parameter & assigned that value to the superclass variable. But In superclass (ViewController2)'s viewDidLoad method, that value is always null. Why ? How can I pass that value from sub to super so that I can get that value in super class's viewDidLoad method ?
ViewController1.h
#interface ViewController : ViewController2
#end
ViewController1.m
#implementation ViewController
- (instancetype)init
{
self = [super initWithName:#"Hello"];
if (self) {
NSLog(#"1 :");
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
ViewController2.h
#interface ViewController2 : UIViewController
#property (nonatomic, strong) NSString *name;
-(instancetype)initWithName:(NSString *)name;
#end
ViewController2.m
- (instancetype)initWithName:(NSString *)name
{
self = [super init];
if (self) {
self.name = name;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"2 : %#", self.name);
}
#end
I tired your code and it worked just fine. I suspect, that you don't instantiate your ViewController directly. Do you use storyboard in your project? If yes - you should override initWithCoder: in ViewController.
However, it is bad idea to set any properties of view controllers during init-family methods. According to Apple recommendations, all customisation should be done in -viewDidLoad or -viewWill/DidAppear methods.
If you absolutely in need to set name property from outside, it is better you assign it directly, not trough passing argument to init method.
One more thing - initWithNibName:bundle: is Designated initializer. It means, that your subclasses absolutely must in the end call it during initialisation chain.
Updated - 16-Mar-2016
Example :
ViewController1.h
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#interface ViewController1 : ViewController2
#end
ViewController1.m
#import "ViewController1.h"
#interface ViewController1 ()
#end
#implementation ViewController1
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil andUserName:#"Ramesh Annadurai"];
// we can use above init method or below method, both will work
self = [super initWithName:#"Test Name"];
if (self) {
[self.view setBackgroundColor:[UIColor brownColor]];
}
return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
//self = [super initWithCoder:aDecoder];
// If you are using storyboard use this initWithCoder method.
self = [super initWithName:#"Test Name"];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
ViewController2.h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andUserName:(NSString *) userName;
- (instancetype)initWithName:(NSString *) userName;
#end
ViewController2.m
#import "ViewController2.h"
#interface ViewController2 ()
#property (strong, nonatomic) NSString *userName;
#end
#implementation ViewController2
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andUserName:(NSString *) userName
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.userName = userName;
}
return self;
}
- (instancetype)initWithName:(NSString *) userName
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
self.userName = userName;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"User Name : %#", self.userName);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
I am using iBeacons manufactured by JAALEE, Liam trying get list go iBeacons near to me but i am not able to get it
// JLEConfigBeacon.m
// Example
//
// Created by jaalee on 14-4-23.
// Copyright (c) 2014年 jaalee. All rights reserved.
//
#import "BeaconList.h"
#import "JLEBeaconDevice.h"
#import "SWRevealViewController.h"
#import "PetFinderViewController.h"
#import "AdvertisementViewController.h"
#interface BeaconList ()
#property (nonatomic) NSMutableArray *mBeaconDeviceList;
#property (nonatomic) JLEBeaconConfigManager *mBeaconConfigManager;
#property (nonatomic) JLEBeaconDevice *mBeaconDevice;
#end
#implementation BeaconList
#synthesize destinationName;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_mBeaconDeviceList = [[NSMutableArray alloc] init];
_mBeaconConfigManager = [[JLEBeaconConfigManager alloc] init];
_mBeaconConfigManager.delegate = self;
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = #selector(revealToggle:);
if ([[self title] isEqualToString:#"Beacon List"]) {
[self setTitle:#"Pet Finder"];
}
}
-(void)viewDidAppear:(BOOL)animated
{
[_mBeaconDeviceList removeAllObjects];
[self.mTableView reloadData];
[_mBeaconConfigManager startJaaleeBeaconsDiscovery];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark - JLEBeaconConfigManager delegate
- (void)beaconConfigManager:(JLEBeaconConfigManager *)manager didDiscoverBeacon:(JLEBeaconDevice *)beacon RSSI:(NSNumber *)RSSI
{
for (int i = 0; i < _mBeaconDeviceList.count; i++) {
JLEBeaconDevice *temp = [_mBeaconDeviceList objectAtIndex:i];
if (temp == beacon) {
return;
}
}
[_mBeaconDeviceList addObject:beacon];
[self.mTableView reloadData];
}
#end
Dont know how to resolve this. i want to use this to get Proximity UUID from detected beacon.
GIT LINK OF SDK
If you ibeacon do not have any additional features as estimote ibeacons(accelerometers and temperature sensor), you must not use SDK.
Look at this tutorial for iOS 8 and change the UUID.
http://ibeaconmodules.us/blogs/news/14279747-tutorial-ibeacon-app-development-with-corelocation-on-apple-ios-7-8
I have an app I am working on, and I am trying to have the logo on for a bit longer, and fade out/slide out/effect when it's done.
Here's my setup:
The Tab Bar controller is not letting me place an Image View inside it, so I created a view to have it on.
I am trying to have the logo stay on for a bit, fade out, then automatically switch the view (Segue) to the Tab Bar controller.
This is what I get out of it: http://youtu.be/l4jL0BfpR2k
So here's my code:
//
// BDNLogoViewController.m
// Bronydom Network
//
// Created by name on 10/1/13.
// Copyright (c) 2013 name. All rights reserved.
//
#import "BDNLogoViewController.h"
#import "BDNTabBarController.h"
#import "BDNFirstViewController.h"
#interface BDNLogoViewController ()
#end
#implementation BDNLogoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[UIView animateWithDuration:1 animations:^{
_imageview.alpha = 0;
}];
//BDNTabBarController *viewController = [[BDNTabBarController alloc] init];
//[self.navigationController pushViewController:viewController animated:YES];
(void)#selector(seguePerform:);
}
- (void)seguePerform:(id)sender
{
//BDNTabBarController *myNewVC = [[BDNTabBarController alloc] init];
// do any setup you need for myNewVC
[self performSegueWithIdentifier:#"open" sender:sender];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Yes, "open" is defined as the segue id.
Do you guys have any ideas on how I could fix this?
To fix, add this
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
[self performSegueWithIdentifier:#"open" sender:self];
}
Remove this from your code
(void)#selector(seguePerform:);
// and all other unnecessary segue stuff you had
I asked a question about dismissing multiple view controllers previously and the answers that i was given along with the possible solutions i found elsewhere have all failed to achieve the desired effect. i have narrowed down my issue to something with the way i set up my delegation. the code is below and i would really appreciate any feedback.
my full project can be downloaded here: https://www.yousendit.com/download/TEhWckhYQVNYSHpIRHNUQw
Thanks.
//
// QuestionViewController.h
// learningTheRopes1
//
// Created by James Ulle on 7/18/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Question.h"
#import "AnswerViewController.h"
#interface QuestionViewController : UIViewController <AnswerViewControllerDelegate>
#property (weak, nonatomic) IBOutlet UILabel *currentQuestionDisplay;
#property (weak, nonatomic) IBOutlet UITextField *userAnswerTextField;
#property (nonatomic, strong) Question *currentQuestion;
- (IBAction)dismissKeyboard:(id)sender;
- (void)dismissQVC;
#end
//
// QuestionViewController.m
// learningTheRopes1
//
// Created by James Ulle on 7/18/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "QuestionViewController.h"
#interface QuestionViewController ()
#end
#implementation QuestionViewController
#synthesize currentQuestionDisplay;
#synthesize userAnswerTextField;
#synthesize currentQuestion;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
AnswerViewController *avc = [segue destinationViewController];
[avc setCurrentQuestion:currentQuestion];
[avc setUserAnswer:[userAnswerTextField text]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.currentQuestionDisplay setText:[currentQuestion question]];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setCurrentQuestionDisplay:nil];
[self setUserAnswerTextField:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)dismissKeyboard:(id)sender {
[userAnswerTextField resignFirstResponder];
}
- (void)dismissQVC {
NSLog(#"Dismiss QVC");
[self.navigationController popViewControllerAnimated:NO];
}
#end
//
// AnswerViewController.h
// learningTheRopes1
//
// Created by James Ulle on 7/18/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Question.h"
#protocol AnswerViewControllerDelegate <NSObject>
- (void)dismissQVC;
#end
#import "QuestionViewController.h"
#interface AnswerViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *displayCurrentAnswer;
#property (nonatomic, strong) Question *currentQuestion;
#property (nonatomic, strong) NSString *userAnswer;
#property (nonatomic, weak) id <AnswerViewControllerDelegate> delegate;
- (IBAction)dismissAnswerVC:(id)sender;
#end
//
// AnswerViewController.m
// learningTheRopes1
//
// Created by James Ulle on 7/18/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "AnswerViewController.h"
#interface AnswerViewController ()
#end
#implementation AnswerViewController
#synthesize displayCurrentAnswer;
#synthesize currentQuestion;
#synthesize userAnswer;
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if([userAnswer isEqualToString:currentQuestion.answer]) {
[self.displayCurrentAnswer setText:#"You are correct!"];
}
else {
[self.displayCurrentAnswer setText:#"You are wrong!"];
}
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[self setDisplayCurrentAnswer:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)dismissAnswerVC:(id)sender {
[self dismissViewControllerAnimated:YES completion:^{
NSLog(#"Dismiss AVC");
[[self delegate] dismissQVC];
}];
}
#end
and finally my output is this (which shows that the completion block in indeed called, but the delegate call back to dimissQVC does not happen:
2012-08-03 19:04:34.235
learningTheRopes1[4165:f803] Dismiss AVC
In the prepareForSegue method, you missed this line:
avc.delegate = self;
I'm trying to use GLKView in UIViewController, my code looks like this
CustomViewController.h
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
#interface CustomViewController : UIViewController
{
NSString * name;
}
#property NSString * name;
CustomViewController.m
#import "CustomViewController.h"
#interface CustomViewController ()
#end
#implementation CustomViewController
#synthesize name;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
CGRect mainScreen = [[UIScreen mainScreen] bounds];
GLKView * viewGL = [[GLKView alloc] initWithFrame:CGRectMake(mainScreen.size.width / 2, mainScreen.size.height / 2, 50, 50)];
viewGL.context = context;
[self.view addSubview:viewGL];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
How can I define the draw/render method of GLKView? and where can I init OpenGL? Any suggestions?
Initialize OpenGL in your viewDidLoad, just as you're currently doing.
Take a look at registering your view controller as the GLKView's delegate. The delegate's glkView:(GLKView *)view drawInRect: method will be invoked whenever a redraw is required.
This tutorial may help.