Question with trying to simply clear a mutablearray in another class in objective C
the code all runs fine, just the data doesnt get wiped....
SecondViewController.h
#interface SecondViewController : UIViewController <UITableViewDelegate,UITextFieldDelegate,ABPeoplePickerNavigationControllerDelegate>
{
AppDelegate *appDelegate;
}
#property (nonatomic, retain) NSMutableArray *contactsArray;
Thats the declaration of the mutablearray...
then from the class that i am trying to delete it.
listViewController.h
#import "SecondViewController.h"
#interface ListViewController : UIViewController <UITableViewDelegate,ABPeoplePickerNavigationControllerDelegate,UITabBarControllerDelegate>{
AppDelegate *appDelegate;
SecondViewController *secondViewControl;
}
#property (nonatomic, retain) IBOutlet UITableView *table;
then in the main file i use the code
[secondViewControl.contactsArray removeAllObjects];
but then when i check the array it has not been emptied...
EDIT:
#implementation ListViewController
#synthesize table;
/*- (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.
appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
secondViewControl = [[SecondViewController alloc]init];
Related
My cpViewController has a button that modal segues to cpScanViewController. I want to have cpScanViewController inform ViewController with a string when it has completed a successful scan. After reading numerous articles online, I believe delegation is the best way to do this?
Is the delegate being set correctly?
How does the delegate inform the cpViewController?
cpScanViewController.h
#import <UIKit/UIKit.h>
#protocol ScanDelegate <NSObject>
-(void)receivedUPC:(NSString *)Scan;
#end
#interface cpScanViewController : UIViewController
#property (nonatomic, weak) id <ScanDelegate> delegate;
#property (nonatomic, retain) NSString *UPC;
-(void)checkUPC;
#end
cpScanViewController.m
#interface cpScanViewController ()
{
NSString *UPC;
}
#end
#implementation cpScanViewController
#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];
// Do any additional setup after loading the view.
}
-(void)doSomeScanStuff
{
UPC = #"a string"
// even tried
[delegate receivedUPC:UPC];
}
-(void)checkUPC
{
}
#end
cpViewController.h
#import <UIKit/UIKit.h>
#import "cpScanViewController.h"
#interface cpViewController : UIViewController <ScanDelegate>
#end
cpViewController.m
#interface cpViewController ()
{
cpScanViewController *cp;
}
#end
#implementation cpViewController
- (void)viewDidLoad
{
// set delegate
cp = [[cpScanViewController alloc] init];
[cp setDelegate:self];
}
-(void)receivedUPC:(NSString *)Scan{
// Nothing happening
NSLog(#"%#", Scan);
NSLog(#"The %#", cp.UPC);
}
#end
Because you are not calling receivedUPC method from your cpScanViewController class. You need to add [_delegate receivedUPC:self], some where in order to invoke this delegat method in cpViewController class.
I was able to get it working by using the method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
cpScanViewController *vd = (cpScanViewController *)[segue destinationViewController];
vd.delegate = self;
}
Because I made the connection in storyboards I didn't realize it needed to have the method.
I can't see the webview when I launch tha app..
In the delegate that's my code
#import <Cocoa/Cocoa.h>
#include "NewsViewController.h"
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic,strong) IBOutlet NSSplitView *splitView;
#property (nonatomic,strong) IBOutlet NewsViewController *newsViewController;
#end
#import "AppDelegate.h"
#implementation AppDelegate
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
// 1. Create the master View Controller
self.newsViewController = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.splitView addSubview:self.newsViewController.view];
}
#end
That's my viewcontroller
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface NewsViewController : NSViewController{
IBOutlet WebView *web;
}
#property (assign) IBOutlet WebView *web;
#end
#import "NewsViewController.h"
#interface NewsViewController ()
#end
#implementation NewsViewController
#synthesize web;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
-(void)loadView{
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[web mainFrame] loadRequest:requestObj];
}
#end
if I out one label on my view in the controller everything is fine but if I put a webview I see only a grey window.
Why this?
thanks
I do believe you need to call [super loadView] in your loadView implementation.
I have a tabbar project in xcode and on the first view i need to find my GPS location and I need to save longitude and latitude on two variables on the appdelegate. Here some code:
FirstViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface FirstViewController : UIViewController <CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
#property (nonatomic, retain) CLLocationManager *locationManager;
#end
FirstViewController.m
#import "FirstViewController.h"
#import "CampeggiandoAppDelegate.h"
#import <CoreLocation/CoreLocation.h>
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize locationManager;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.image = [UIImage imageNamed:#"ic_home2"];
self.tabBarItem.title=#"Home";
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
self.locationManager.distanceFilter=500.0f;
self.locationManager.desiredAccuracy=kCLLocationAccuracyHundredMeters;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CampeggiandoAppDelegate *appDelegate=(CampeggiandoAppDelegate*)[[UIApplication sharedApplication]delegate];
appDelegate.latitudineDel=[NSString stringWithFormat:#"%3.5f", newLocation.coordinate.latitude];
appDelegate.longitudineDel=[NSString stringWithFormat:#"%3.5f", newLocation.coordinate.longitude];
}
When I run the compiler and the fitst view comes up the application broke and appears this exception:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[AppDelegate
setLatitudineDel:]: unrecognized selector sent to instance 0x84210b0'
Any help? Thanks.
Your class interface should look like this before setting variables the way you do:
#interface CampeggiandoAppDelegate : UIResponder <UIApplicationDelegate> {
// ...
// some ivars
// ...
}
#property (nonatomic, strong) NSString *latitudineDel;
#property (nonatomic, strong) NSString *longitudineDel;
#end
What you have for this moment is this:
#interface CampeggiandoAppDelegate : UIResponder <UIApplicationDelegate> {
// ...
NSString *latitudineDel;
NSString *longitudineDel
// ...
}
#end
So there are no setters for those instance variables, that's why the exception is thrown.
For more information about properties read this.
Im new to Objective and iPad apps. Im trying to display an array that Ive already used in a separate Class/View Controller. The array Im trying to access is in "MainMenuScreen.m" and I want to use that array in another class called "ScoresScreen.m".
All my code is working, I just need to get the array from the other class and plug into my code.I tried using Segue but im getting this error in my code, and im stuck. Thankyou in advance
.h
#import <UIKit/UIKit.h>
#interface ScoresScreen : UIViewController<UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *scoresArray;
}
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (weak, nonatomic) NSMutableArray *scoresArray; //Added this line
#end
.m
#import "ScoresScreen.h"
#import "MainMenuScreen.h"
#interface ScoresScreen ()
#end
#implementation ScoresScreen
#synthesize scoresArray; //Added this line
NSMutableArray *TestScoresArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
...
....
....
.m
#import "MainMenuScreen.h"
#import "PlayGame.h"
#import "ImageTest.h"
#import "ScoresScreen.h"
#interface MainMenuScreen ()
#end
#implementation MainMenuScreen
//---------------------------------------------------------------------------------
//------------------------------------Variables------------------------------------
PlayGame *playGame;//PlayGameObject
NSMutableArray *gameDataArray;//Game Data Array (A array storying multable ImageTest Objects)
NSMutableArray *scoresArray;//<-------This need to go to view scores
//---------------------------------------------------------------------------------
//----------------------------------Constructors-----------------------------------
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
scoresArray = [[NSMutableArray alloc] init];
[scoresArray addObject: [NSMutableArray arrayWithObjects:#"Bob",[NSNumber numberWithInt:134],nil]];//testData
[scoresArray addObject: [NSMutableArray arrayWithObjects:#"Roger",[NSNumber numberWithInt:12],nil]];//testData
[scoresArray addObject: [NSMutableArray arrayWithObjects:#"Ben",[NSNumber numberWithInt:34],nil]];//testData
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
//---------------------------------------------------------------------------------
//----------------------------------Add New Score----------------------------------
//a setter Method so Play Game can add the user name and score to the scoresArray
-(void)AddNewScore :(NSString *)userName :(int)score
{
//adds user name and score to the array
if (scoresArray != nil)
{
/*NSMutableArray *nameAndScore = [[NSMutableArray alloc] init];
[nameAndScore addObject:userName];
[nameAndScore addObject: [NSNumber numberWithInt:score]];//int needs to be converted before storing in an array
[scoresArray addObject:nameAndScore];*/
[scoresArray addObject: [NSMutableArray arrayWithObjects:userName,[NSNumber numberWithInt:score],nil]];
for(NSMutableArray *m in scoresArray)
{
NSLog(#"%#%#", m[0],m[1]);//prints out the array in logs to make sure it is working
}
}
}
//added this method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ScoresScreen *controller = [segue destinationViewController];
controller.array = yourArray; //use of undeclared identifier 'yourarray'
//and this error "property 'array' not found on object of type 'ScoresScreen*'"
}
//This is where I get the error ^.
...Please can someone help me? Thanks
---------
Use this in your MainMenuScreen.m file
#import "MainMenuScreen.h"
#import "PlayGame.h"
#import "ImageTest.h"
#import "ScoreScreen.h"
Working over storyboard.
I've created View Controller (storyboard) and then in middle of content added subview from xib file.
I want to add xib (UIView) like as subview into ViewController and send object with data and print that data into label but I don't know how.
Here is my code.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
UIView *subView;
}
#property (strong, nonatomic) IBOutlet UIView *subView; //connected over IB
#end
ViewController.m
#import "OfferViewController.h"
#import "OfferLocation.h"
#interface OfferViewController ()
#end
#implementation OfferViewController
#synthesize subView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
OfferLocation *location = [[OfferLocation alloc] initWithFrame:CGRectZero];
[self.subView addSubview:location];
}
...
#end
and here is subview:
OfferLocation.h
#import <UIKit/UIKit.h>
#interface OfferLocation : UIView{
UIView *view;
UILabel *locationLabel;// here is that label taht I want to print into
}
#property (nonatomic, retain) IBOutlet UIView *view;// connected over IB
#property (nonatomic, retain) IBOutlet UILabel *locationLabel;
#end
OfferLocation.m
#import "OfferLocation.h"
#implementation OfferLocation
#synthesize view, locationLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
locationLabel.text = #"some text"; //this is not working
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:#"OfferLocation" owner:self options:nil];
UIView *tempView = [subviewArray objectAtIndex:0];
[self addSubview:tempView];
}
return self;
}
subView never seems to be initialized and loaded to the view or presented. Therefore you cannot see it.
Try calling:
self.view = self.subView // or subView; // or [self.view addSubview:self.subView // or subview];
or something similiar
Then declare a UILabel and initialize it with whatever text you want and add it to subview:
UILabel *label = [[UILabel alloc] initWithString:#"StackOverflow rocks!"];
[self.subView addSubview:label];
// or [self.view addSubview:label];
Differing on how you set subView.
What is this data you want to set on label's text? If it is a data type use the specifiers in the string.
%i // for int
%# // for string or object adress
%c // for char
%f // for float
etc...