【发布时间】:2015-06-13 20:45:01
【问题描述】:
我试图让我的 iOS 应用程序从服务器上的文本文件中提取并在文本视图中显示它。如果我只是通过在 viewDidLoad 方法中设置 url 来做到这一点,这很好用。但是,如果我按照我需要的方式进行操作,则单击按钮会调用一个方法,该方法根据单击的按钮设置 url,然后在移动到视图控制器时也填充 Text 视图,然后 Text 视图不会收到任何文本.
我不确定这是否与它有关,但可能是因为我正在使用一个按钮,该按钮具有触发的 segue 以移动到下一个视图控制器,然后发送事件以拉动信息?会不会是乱码之类的?
这是我的代码:
#import "ViewController.h"
#import "STTwitter.h"
@interface ViewController ()
{
STTwitterAPI *twitter;
}
@property (weak, nonatomic) IBOutlet UITextView *scheduleText;
@property (weak, nonatomic) IBOutlet UITextView *tweetText;
@property (weak, nonatomic) IBOutlet UIScrollView *mScrollView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//pull from server to populate schedule
//[self pullInfo:(1)];
//load tweets
//[self callTwitter];
//set scroll view size
_mScrollView.contentSize = CGSizeMake(320, 300);
}//end viewDidLoad
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}//end didReceiveMemoryWarning
- (void) pullInfo: (int) page
{//pull information from server
NSError* err;
NSURL* url = nil;
if (page == 1)
url = [NSURL URLWithString:@"http://www.mckendree.edu /aecschedule1.txt"];
else if (page == 2)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule2.txt"];
else if (page == 3)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule3.txt"];
else if (page == 4)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule4.txt"];
else if (page == 5)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule5.txt"];
else if (page == 6)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule6.txt"];
else if (page == 7)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule7.txt"];
else if (page == 8)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule8.txt"];
else if (page == 9)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule9.txt"];
else if (page == 10)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule10.txt"];
else if (page == 11)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule11.txt"];
else if (page == 12)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule12.txt"];
else if (page == 13)
url = [NSURL URLWithString:@"http://www.mckendree.edu/aecschedule13.txt"];
NSData *htmlData = [NSData dataWithContentsOfURL:url];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:htmlData options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
//retrieve the text if there was no reading error
if (err != nil)
printf("Error retrieving text");
else
{
[_scheduleText setAttributedText:attrString];
[_scheduleText sizeToFit];
[_scheduleText setTextColor:[UIColor whiteColor]];
}//end else
}//end pullInfo
- (IBAction)settingsClicked:(id)sender
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"McK AEC" message:@"Developed by: Sean Boehnke" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}//end settingsClicked
- (IBAction)time1Clicked:(id)sender
{
[self pullInfo:(1)];
}//end time1Clicked
- (IBAction)time2Clicked:(id)sender
{
[self pullInfo:(2)];
}//end time2Clicked
- (IBAction)time3Clicked:(id)sender
{
[self pullInfo:(3)];
}//end time3Clicked
- (IBAction)time4Clicked:(id)sender
{
[self pullInfo:(4)];
}//end time4Clicked
- (IBAction)time5Clicked:(id)sender
{
[self pullInfo:(5)];
}//end time5Clicked
- (IBAction)time6Clicked:(id)sender
{
[self pullInfo:(6)];
}//end time6Clicked
- (IBAction)time7Clicked:(id)sender
{
[self pullInfo:(7)];
}//end time7Clicked
- (IBAction)time8Clicked:(id)sender
{
[self pullInfo:(8)];
}//end time8Clicked
- (IBAction)time9Clicked:(id)sender
{
[self pullInfo:(9)];
}//end time9Clicked
- (IBAction)time10Clicked:(id)sender
{
[self pullInfo:(10)];
}//end time10Clicked
- (IBAction)time11Clicked:(id)sender
{
[self pullInfo:(11)];
}//end time11Clicked
- (IBAction)time12Clicked:(id)sender
{
[self pullInfo:(12)];
}//end time12Clicked
- (IBAction)time13Clicked:(id)sender
{
[self pullInfo:(13)];
}//end time13Clicked
@end
【问题讨论】:
-
1.始终尝试发布尽可能少的代码。 2. 你确定 URL 确实返回了有效数据吗? 3.尝试设置textView的背景颜色,看看frame设置是否正确。
-
@iosDev82 是的,它返回数据。正如我在帖子中所说,如果我只是直接从 viewDidLoad 方法调用 pullInfo 方法,那么它会很好。这就是为什么我觉得将其移至视图控制器并同时调用单独的方法时遇到问题。
-
问题不清楚。我可以看到许多 timeclicked 动作,但没有一个执行 segue。或者这个 viewcontroller 是 segued 并且数据没有填充到 textview 中的那个?
-
@UttamSinha 我正在使用情节提要,这是我编辑过任何代码的唯一文件。我只需要从不同的 url 中提取每个按钮并在我移动的下一个视图控制器上填充文本视图从情节提要。
-
@saboehnke - 如果是这种情况,则在下一个视图控制器中定义属性 nstring 并在第一个视图控制器中导入下一个视图控制器,以实例为例,当您转到下一个视图控制器时,将获取的数据分配给 nsstring。
标签: ios objective-c textview storyboard viewcontroller