【问题标题】:Can't get text retrieved from plist via pickerView to scroll in UITextView within UIScrollView无法通过 pickerView 从 plist 检索文本以在 UIScrollView 内的 UITextView 中滚动
【发布时间】:2012-10-15 15:02:51
【问题描述】:

我有一个由导航控制器锚定的多屏应用程序。在一个屏幕上,输入到 UIScrollView 内的 UILabel 中的静态文本可以完美滚动。在另一个屏幕上,我按照相同的格式在它自己的 UIScrollView 中创建了一个可滚动的 UILabel - 只是这次滚动文本会根据从 plist 文件中提取的内容而有所不同,当在同一屏幕上的 pickerView 中进行选择时,该文件会被访问。结果是 - 文本出现,并在选择新的选择器项时正确更改 - 但文本不会滚动。

我找到了this solution,但我不知道如何将其应用于我的情况。

我还阅读了有关使用 UITextView 而不是 UILabel 的信息,但我无法弄清楚如何在 Xcode (v.4.5) 的情节提要上的 Connections Inspector 中为其创建引用出口。我认为可能有一种方法可以对此进行编码,而不是依赖图形界面。

这是我的实现文件的相关部分。在pickerView中选择了“Term”,而UILabel中出现了“Definition”:

@implementation InsuranceGlossaryViewController
@synthesize termPicker, termArray, definitionArray;
@synthesize termLabel, definitionLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(280, 2400)];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Glossary" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.termArray = [dict objectForKey:@"Term"];
    self.definitionArray = [dict objectForKey:@"Definition"];

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.termArray = nil;
    self.definitionArray = nil;
    self.termLabel = nil;
    self.definitionLabel = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
    return [termArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    return [termArray objectAtIndex:row];
} 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{
    NSString *resultString = [[NSString alloc] initWithFormat:
                              @"%@",
                              [definitionArray objectAtIndex:row]];
    definitionLabel.text = resultString;
    NSString *termString = [[NSString alloc] initWithFormat:
                              @"%@",
                              [termArray objectAtIndex:row]];
    termLabel.text = termString;

}

这是头文件中的代码:

@interface InsuranceGlossaryViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource, MFMailComposeViewControllerDelegate>
{
    UIPickerView       *termPicker;
    NSArray            *termArray;
    NSArray            *definitionArray;
    UILabel            *termLabel;
    UILabel            *definitionLabel;
    IBOutlet UIScrollView *scroller;
}
- (IBAction)showEmail:(id)sender;
@property (strong, nonatomic) IBOutlet UIPickerView *termPicker;
@property (strong, nonatomic) IBOutlet UILabel *termLabel;
@property (strong, nonatomic) IBOutlet UILabel *definitionLabel;
@property (strong, nonatomic) NSArray *termArray;
@property (strong, nonatomic) NSArray *definitionArray;

任何帮助将不胜感激。

编辑:我似乎已经启动并运行了一个 UITextView 字段 - 至少当我在其中滑动时我可以看到它滚动。我不知道如何让“definitionLabel”出现在其中。使用 UILabel,我能够为定义标签设置一个引用插座。但是 UITextView 的工作方式不同。我很难过,我的老板希望这个应用程序在这个月上线!

【问题讨论】:

    标签: xcode uiscrollview uilabel plist uipickerview


    【解决方案1】:

    解决了。

    我添加了“UITextFieldDelegate”和“IBOutlet UITextView *textView;”到.h文件并添加“textView.text = resultString;”到 .m 文件,这使我可以将 UITextView 的引用出口与 textView 联系起来。现在 UITextView 中出现了正确的文本并且它会滚动。

    【讨论】:

      猜你喜欢
      • 2015-11-17
      • 2019-01-31
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多