iphone Pass String to another .m file in project

梦想的初衷 提交于 2019-12-12 01:24:24

问题


ViewController.m will assemble the body of an email as the user pushes buttons and types in textviews. The program that reads the email asks that it is in XML. The final message will look like this:

NSString *sendMessage = [[NSString alloc]initWithFormat:@"<?xml version = \"1.0\" ?>\n<?commitcrmxml version = \"1.0\" ?>\n<CommitCRMTransaction>\n<ExternalApplicationName>Myapp</ExternalApplicationName>\n<SendResponseToEmail>err@mysite.com</SendResponseToEmail>\n<Password>pass</Password>\n<ReturnTransactionID>CRDOWV34HL53J543GENDYDH92BSF</ReturnTransactionID>\n<DataKind>TICKET</DataKind>\n<RecordData>\n<FLDTKTCARDID>%@</FLDTKTCARDID>\n<FLDTKTPROBLEM>%@\n%@\n%@\n%@\n%@\n%@</FLDTKTPROBLEM>\n<FLDTKTSTATUS>100</FLDTKTSTATUS>\n<FLDTKTKIND>General</FLDTKTKIND>\n<FLDTKTPRIORITY>10</FLDTKTPRIORITY>\n<FLDTKTSOURCE>Myapp</FLDTKTSOURCE>\n<FLDTKTSCHEDLENESTIM>60</FLDTKTSCHEDLENESTIM>\n<FLDTKTFORDISPATCH>N</FLDTKTFORDISPATCH>\n</RecordData>\n</CommitCRMTransaction>", cardID, tempStoreCompany, tempStoreLocation, tempStoreName, tempStorePhone, tempStoreEmail, descriptionMessage];

My second implementation file, MailSend.m, Is going to send the message using (SKP)SMTP. MailSend.m needs access to the text in the sendMessage string (in ViewController.m) so that the message may be sent properly.

How can I do this?


回答1:


Make a property

@property (nonatomic,retain) NSString *sendText;

in .h file and synthesize in .m file as

@synthesize sendText;

Then set it where you are allocating your MailSend object like

MailSend *ms = [[MailSend alloc] init....];
ms.sendText = sendMessage;
[self present...];
//or self.navigationController pushVie...];

and access this string using sendText in MailSend.m




回答2:


There are three simple ways that you can go about doing this.


The first is how Inder stated in which you create a property and set it.


The second way would be to create a method that accepts a NSString as s parameter in your SecondViewController.

SecondViewController.h

- (void)setTextBody:(NSString*)_body;

SecondViewController.m

- (void)setTextBody:(NSString*)_body {
localBodyString = _body;
}

FirstViewController.m

SecondViewController *second = [[SecondViewController alloc] init...];
[second setTextBody:sendMessage];
//Push the view controller

Another way to do this would be to add a new init method to the SecondViewController class that accepts a NSString.

SecondViewController.h

- (id)initWithString:(NSString*)_body;

SecondViewController.m

- (id)initWithString:(NSString*)_body {
if (self)
 localBodyString = _body;
return self;
}

FirstViewController.m

SecondViewController *second = [[SecondViewController alloc] initWithString:sendMessage];
//Push view controller

Now if both of these you will need to define a NSString *localBodyString variable in the header file.



来源:https://stackoverflow.com/questions/11507906/iphone-pass-string-to-another-m-file-in-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!