simple question - sending NSString to another class - iphone

//StringStringViewController.h
#import <UIKit/UIKit.h>
#import "MyClass.h"
#interface SendStringViewController : UIViewController {
NSString *string1;
MyClass *secondview;
}
#property (nonatomic, retain) NSString *string1;
#property (nonatomic, retain) MyClass *secondview;
-(IBAction)sendString1:(id)sender;
#end
// SendStringViewController.m
#import "SendStringViewController.h"
#implementation SendStringViewController
#synthesize string1;
#synthesize secondview;
-(IBAction)sendString1:(id)sender {
string1 = #"firststring";
secondview.string2 = string1;
MyClass *mc = [[MyClass alloc]init];
[self presentModalViewController:mc animated:YES];
}
#end
// MyClass.h
#import <UIKit/UIKit.h>
#interface MyClass : UIViewController {
NSString *string2;
}
#property (nonatomic, retain) NSString *string2;
#end
// MyClass.m
#import "MyClass.h"
#implementation MyClass
#synthesize string2;
- (void)viewDidLoad {
NSLog(#"%#", string2);
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
#end
I know it's a simple and easy task but I'm having hard time googling it, because it's way too easy.
Here's the link of my project too.
Thanks.
http://dl.dropbox.com/u/12439052/SendString.zip
Edit: the question is on the title actually but how do i send the value of string1 to string2 cause currently when i nslog the string2 only null comes out

-(IBAction)sendString1:(id)sender {
string1 = #"firststring";
MyClass *mc = [[MyClass alloc]init];
mc.string2 = string1;
[self presentModalViewController:mc animated:YES];
}

Related

Add subtitle to annotation using UICRouteAnnotation

UICRouteAnnotation *startAnnotation = [[UICRouteAnnotation alloc] initWithCoordinate [[routePoints objectAtIndex:0] coordinate]
title:#"Origin"
subtitle:#"Subtitle Here"
annotationType:UICRouteAnnotationTypeStart];
I am trying to add a subtitle to this annotation but don't have much experience with UICRouteAnnotation and was not able to find much documentation on it. Adding "subtitle:" through exceptions.
What am I missing? Why doesn't the subtitle work when doing it like this?
What I didn't realize is that UICRouteAnnotation needed to be modified to accept a subtitle. Here is my updated h and m files for UICRouteAnnotation. Making the below changes fixed my issue so the code I posted in my question works as intended.
Thanks Anna for pointing me in the right direction.
.h file
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
typedef enum UICRouteAnnotationType {
UICRouteAnnotationTypeStart,
UICRouteAnnotationTypeEnd,
UICRouteAnnotationTypeWayPoint,
} UICRouteAnnotationType;
#interface UICRouteAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
UICRouteAnnotationType annotationType;
}
#property (nonatomic) CLLocationCoordinate2D coordinate;
#property (nonatomic, readonly, copy) NSString *title;
#property (nonatomic, readonly, copy) NSString *subtitle;
#property (nonatomic) UICRouteAnnotationType annotationType;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord
title:(NSString *)aTitle
subtitle:(NSString *)aSubTitle
annotationType:(UICRouteAnnotationType)type;
#end
.m file
#import "UICRouteAnnotation.h"
#implementation UICRouteAnnotation
#synthesize coordinate;
#synthesize title;
#synthesize subtitle;
#synthesize annotationType;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord
title:(NSString *)aTitle
subtitle:(NSString *)aSubTitle
annotationType:(UICRouteAnnotationType)type {
self = [super init];
if (self != nil) {
coordinate = coord;
title = [aTitle retain];
subtitle = [aSubTitle retain];
annotationType = type;
}
return self;
}
- (void)dealloc {
[title release];
[subtitle release];
[super dealloc];
}
#end

Memory leak in a class with 3 NSMutableArrays. Why?

I'm getting a memory leak with instruments in a class that I've created. This is the class:
.h
#import <Foundation/Foundation.h>
#interface RDItem : NSObject {
}
#property int Id;
#property (nonatomic, retain) NSString *nombre;
#property (nonatomic, retain) NSString *thumbnail;
#property (nonatomic, retain) NSString *thumbnailPush;
#property int defaultColorId;
#property int idTema;
#property (nonatomic, retain) NSString *selectedFrame;
#property (nonatomic, retain) NSString *mergedFrame;
#property (nonatomic, retain) NSMutableArray *colors;
#property (nonatomic, retain) NSMutableArray *textures;
#property (nonatomic, retain) NSMutableArray *styles;
-(void)initialize;
#end
.m
#import "RDItem.h"
#implementation RDItem
#synthesize Id;
#synthesize nombre;
#synthesize thumbnail;
#synthesize thumbnailPush;
#synthesize defaultColorId;
#synthesize idTema;
#synthesize selectedFrame;
#synthesize mergedFrame;
#synthesize colors;
#synthesize textures;
#synthesize styles;
-(void)initialize
{
colors = [[NSMutableArray alloc] init];
textures = [[NSMutableArray alloc] init];
styles = [[NSMutableArray alloc] init];
}
-(void)dealloc
{
[colors release];
[textures release];
[styles release];
}
#end
This class have 3 NSMutableArray where I'll store data. In order to prepare and initialice this class, I've developed the method initialize where the 3 arrays are created. In dealloc are released.
The leaks tool detects a leak each time this class is used because the initialize method.
Which is the best way to initialize these arrays?
Thanks.
EDIT
Hi I've solved the leak with RDItem but now appears another lear in a very similar class:
.h
#import <Foundation/Foundation.h>
#interface RDTema : NSObject {
}
#property int Id;
#property (nonatomic, retain) NSString *idManifest;
#property (nonatomic, retain) NSString *idTema;
#property (nonatomic, retain) NSString *nombre;
#property (nonatomic, retain) NSString *thumbnail;
#property (nonatomic, retain) NSString *thumbnailPush;
#property (nonatomic, retain) NSMutableArray *items;
#property (nonatomic, retain) NSMutableArray *colors;
#property (nonatomic, retain) NSMutableArray *textures;
#property (nonatomic, retain) NSMutableArray *styles;
-(void)initialize;
#end
.m
#import "RDTema.h"
#implementation RDTema
#synthesize Id;
#synthesize idManifest;
#synthesize idTema;
#synthesize nombre;
#synthesize thumbnail;
#synthesize thumbnailPush;
#synthesize items;
#synthesize colors;
#synthesize textures;
#synthesize styles;
-(void)initialize
{
/*
self.items = [[NSMutableArray alloc] init];
self.colors = [[NSMutableArray alloc] init];
self.textures = [[NSMutableArray alloc] init];
self.styles = [[NSMutableArray alloc] init];
*/
self.items = [NSMutableArray array];
self.colors = [NSMutableArray array];
self.textures = [NSMutableArray array];
self.styles = [NSMutableArray array];
}
-(void)dealloc
{
[idManifest release];
[idTema release];
[nombre release];
[thumbnail release];
[thumbnailPush release];
[items release];
[colors release];
[textures release];
[styles release];
[super dealloc];
}
Now I'm getting a leak in these lines:
self.items = [NSMutableArray array];
self.colors = [NSMutableArray array];
self.textures = [NSMutableArray array];
self.styles = [NSMutableArray array];
Anyone can explain why is happening in this class and not in RDItem class now? Are the same :(
Thanks.
This is a better suggested implementation
-(void)initialize
{
self.colors = [NSMutableArray array];
self.textures = [NSMutableArray array];
self.styles = [NSMutableArray array];
}
-(void)dealloc
{
self.colors = nil;
self.textures = nil;
self.styles = nil;
}
I think that you are getting the leak because you call your initialize message more than once and you are not releasing the variables.
Normally, you should use setters or getters to access your ivars, so the appropiate operations are called (like the release message before assigning a new value on setter).
And remember to call [super dealloc] as the last instruction for your dealloc ;)
Without seeing the code where you are initialising the RDItem i can't say for sure.
But i expect that you are not releasing your previous RDItem before creating your new one. So post the code where you are creating your RDItems so we can say for sure
I think, that leak are in arrays filling code, not init. For example, you alloc and init some NSString and put it in your NSMutable array without release or autorelease it. Or something like this. Make shure that you filling your arrays right way.
Just init them when you need it. Calling alloc and init holds to arrays whose content is nil. Call alloc initWithObjects: and fill the arrays.

How to copy an array to appdelegate array and then retrieve it again elsewhere?

I want to store the array of urls in my appdelegate array i.e. logoArray from myMutableArray and then use it in other viewcontroller, but i am uable to copy as may be i am doing shallow copy, i have tried othes ways also like initwithArray:copyItems.
code:-
#class FirstViewController;
#interface AppDelegate_iPhone : NSObject <UIApplicationDelegate> {
UIWindow *window;
FirstViewController *viewController;
NSMutableArray *logoArray;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) NSMutableArray *logoArray;
#end
// NO initialization of logoArra is done in .M file
#class AppDelegate_iPhone;
#interface FirstViewController : UIViewController {
NSMutableArray *array;
NSString *logoString;
AppDelegate_iPhone *appDelegate;
}
#end
#implementation FirstViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
int x=5,y=10;
UIApplication *app = [UIApplication sharedApplication];
appDelegate=app.delegate;
NSLog(#" Array ====== %d",[appDelegate.logoArray count]);
array = [[NSMutableArray alloc]initWithArray:appDelegate.logoArray];
NSLog(#"array at 0 ===== %#",[array objectAtIndex:0]);
for (int i=0; i<[array count]; i++) {
logoString = [array objectAtIndex:i];
NSLog(#"%#",logoString);
UIImage *imageFromUrl = [UIImage imageWithContentsOfFile:[NSURL fileURLWithPath:logoString]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:imageFromUrl];
[imgView setFrame:CGRectMake(x, y, 196, 90)];
[self.view addSubview:imgView];
// UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onTapImage)];
// [imgView addGestureRecognizer:tgr];
// [tgr release];
//Do the rest of your operations here, don't forget to release the UIImageView
x = x + 200;
// [imgView release];
}
}
#class Litofinter,AppDelegate_iPhone;
#interface ParsingViewController : NSObject<NSXMLParserDelegate> {
NSString *myString;
NSMutableArray *myMutableArray;
Litofinter *obj;
NSString *currentElement;
AppDelegate_iPhone *appDelegate;
}
#property(nonatomic, retain) NSString *myString;
#property(nonatomic, retain) NSArray *myMutableArray;
#end
#import "ParsingViewController.h"
#import "Litofinter.h"
#import "AppDelegate_iPhone.h"
#implementation ParsingViewController
#synthesize myMutableArray, myString;
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
myMutableArray = [[NSMutableArray alloc]init];
}
// I have parsed here my XML and array gets stored in myMutableArray
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
UIApplication *app = [UIApplication sharedApplication];
appDelegate=app.delegate;
appDelegate.logoArray = [[NSMutableArray alloc]initWithArray:myMutableArray];
// NSLog(#"appDelegate.logoArray count %d",[appDelegate.logoArray count]);
for (Litofinter *lito in appDelegate.logoArray) {
NSLog(#"Array Elements :----- %#",lito.cLogo);
}
}
Personally I wouldn't create an array in a viewcontroller and then store it in the appdelegate. I'd be more inclined to create a model for the data ( a class that gets and stores the data and provides it to the view controllers).
this thread may help:
iPhone: Using a NSMutableArry in the AppDelegate as a Global Variable

Getting leaks from substringWithRange

I'm getting a leak from using substringWithRange as shown in the line of code below. I though all these functions were Autorelease and you didnt need to alloc/release them manually.
NSCFString is the object being leaked.
What is it I am doing wrong?
aLTR.drew = [substring substringWithRange:NSMakeRange(match.location+1, (match2.location-(match.location+1)))];
What I am trying to do is extract a substring and store it into my storage class. Code for that below.
#import <Foundation/Foundation.h>
#interface LeagueTableRow : NSObject
{
NSString *_teamName;
NSString *_played;
NSString *_won;
NSString *_drew;
NSString *_lost;
NSString *_goalsFor;
NSString *_goalsAgainst;
NSString *_points;
}
#property(nonatomic, copy) NSString *teamName;
#property(nonatomic, copy) NSString *played;
#property(nonatomic, copy) NSString *won;
#property(nonatomic, copy) NSString *drew;
#property(nonatomic, copy) NSString *lost;
#property(nonatomic, copy) NSString *goalsFor;
#property(nonatomic, copy) NSString *goalsAgainst;
#property(nonatomic, copy) NSString *points;
-(id)init;
#end
#import "LeagueTableRow.h"
#implementation LeagueTableRow
#synthesize teamName = _teamName;
#synthesize played = _played;
#synthesize won = _won;
#synthesize drew = _drew;
#synthesize lost = _lost;
#synthesize goalsFor = _goalsFor;
#synthesize goalsAgainst = _goalsAgainst;
#synthesize points = _points;
-(id)init
{
self = [super init];
return self;
}
-(void) dealloc
{
self.teamName = nil;
self.played = nil;
self.won = nil;
self.drew = nil;
self.lost = nil;
self.goalsFor = nil;
self.goalsAgainst = nil;
self.points = nil;
[super dealloc];
}
#end
I'm quiet surprised I got some leaks I though I was managing the memory quiet tidily.
Thanks for the advice and tips.
-Code
In your dealloc, simply release all the string ivars:
[_teamName release];
etc...
Alternatively, you could do:
[self.teamName release];
etc...
I prefer to use the ivars directly, in such situations.

Iphone SDK:[error]Cannot find protocol declaration for "NSNetServiceDelegate"

this code is from a sample
in .h part :
#interface ViewController : UITableViewController < NSNetServiceBrowserDelegate > {
NSMutableArray * tableData;
NSNetServiceBrowser * _browser;
NSMutableArray * _foundServices;
NSURLConnection * _connection;
NSInputStream * _consumerStream;
NSString * controllerHostName;
}
#property (nonatomic, retain) NSMutableArray * tableData;
#property (nonatomic, retain) NSNetServiceBrowser * _browser;
#property (nonatomic, retain) NSMutableArray * _foundServices;
#property (nonatomic, retain) NSURLConnection * connection;
#property (nonatomic, retain) NSInputStream * consumerStream;
#property (nonatomic, retain) NSString * controllerHostName;
in the .m part
#import "ViewController.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <CFNetwork/CFNetwork.h>
#synthesize tableData;
#synthesize _browser;
#synthesize _foundServices;
#synthesize consumerStream = _consumerStream;
#synthesize connection = _connection;
#synthesize controllerHostName;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
_browser = [[NSNetServiceBrowser alloc] init];
[_browser setDelegate:self];
[_browser searchForServicesOfType:#"_service._tcp" inDomain:#""];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_browser stop];
_browser.delegate = nil;
[_browser release];
_browser = nil;
[_foundServices removeAllObjects];
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didFindService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing
{
self.controllerHostName = [NSString stringWithFormat:#"%#.%#", netService.name, netService.domain];
NSLog(#"ControllerHost String is: %#", self.controllerHostName);
NSLog(#"URL to use is === %#.%#", netService.name, netService.domain);
if (!_foundServices) {
_foundServices = [[NSMutableArray alloc] init];
}
[_foundServices addObject:netService];
[self.tableView reloadData];
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didRemoveService:(NSNetService *)netService moreComing:(BOOL)moreServicesComing
{
[_foundServices removeObject:netService];
}
- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)aNetServiceBrowser
{
[_foundServices removeAllObjects];
}
I declaration all the header as the sample does
But I got error message "Cannot find protocol declaration for "NSNetServiceDelegate"
in .h
interface ViewController : UITableViewController < NSNetServiceBrowserDelegate >
So did I missing anything to declaration ?
The sample doesn't has any warning or error
I thought it's defined in "NSNetServices.h" instead?
Add this to your header:
#import <Foundation/NSNetServices.h>
I got this from the BonjourWeb Apple sample code project.
The problem is a unused function. Just delete:
[self.tableView reloadData];
It will be fine!
I found something like answer ,JUST REMOVE
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_browser stop];
_browser.delegate = nil;
[_browser release];
_browser = nil;
[_foundServices removeAllObjects];
}
The program will get Netservice I want...
WHY ?