在另一个方法中使用viewDidLoad中创建的NSString变量 [英] Use NSString variable created in viewDidLoad inside another method

查看:87
本文介绍了在另一个方法中使用viewDidLoad中创建的NSString变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 viewDidLoad 方法中,我设置了以下变量:

In my viewDidLoad method, I set the following variables:

// Get requested URL and set to variable currentURL
NSString *currentURL = self.URL.absoluteString;
//NSString *currentURL = mainWebView.request.URL.absoluteString;
NSLog(@"Current url:%@", currentURL);

//Get PDF file name
NSArray *urlArray = [currentURL componentsSeparatedByString:@"/"];
NSString *fullDocumentName = [urlArray lastObject];
NSLog(@"Full doc name:%@", fullDocumentName);

//Get PDF file name without ".pdf"
NSArray *docName = [fullDocumentName componentsSeparatedByString:@"."];
NSString *pdfName = [docName objectAtIndex:0];

我希望能够在另一种方法中使用这些变量(即 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

I would like to be able to use these variables inside of another method (i.e. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {)

如何重用这些变量viewDidLoad方法?我是新手......非常感谢帮助

How can I reuse these variables outside of the viewDidLoad method? I'm a newbie... help would be SO much appreciated

推荐答案

让它们成为实例变量而不是您正在使用的方法的本地变量。之后,您可以从同一类的所有方法访问它们。

Make them an instance variable and not a variable local to the method you're using. After that, you can access them from all methods of the same class.

示例:

@interface MyClass: NSObject {
    NSString *currentURL;
    // etc.
}

- (void)viewDidLoad
{
    currentURL = self.URL.absoluteString;
    // etc. same from other methods
}

这篇关于在另一个方法中使用viewDidLoad中创建的NSString变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆