繁体   English   中英

使用IBAction的if语句更改UITextView

[英]Changing UITextView with if statement from IBAction

我一直在搜索很多东西,但似乎无法弄清楚。 我对iOS开发很陌生,如果我做的事情完全错误,我深表歉意。

这是情况。

我试图通过按钮切换我的DetailViewController的内容视图,该按钮应将您带到下一个文本视图或具有不同文本的相同视图。

这是我的头文件。

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextView *rowContent;
@property int rowNumber;
@property (strong, nonatomic) NSString * rowName;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *rightArrow;

@end

我的tableViewController根据选择的单元格将我重定向到DetailViewContoller。 现在,在DetailViewController中,我还有另一个按钮想要切换到下一个文本,而不是回到tableViewController并选择其他单元格。

 //DetailViewController
//set the title of the view
self.title = rowName;

//set the UITextView basec on rowNumber
switch (rowNumber) {
    case 0:
        rowContent.text = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"file" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
        break;
    case 1:
        rowContent.text = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"file1" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
        break;
    case 2:
        rowContent.text = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"file2" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
        break;
    case 3:
        rowContent.text = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"file3" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
        break;
    default:
        break;


//TableViewController
 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//create and instance of our DetailViewController
DetailViewController * DVC = [[DetailViewController alloc]init];
//set the DVC to the destinationViewContoller
DVC = [segue destinationViewController];
//get the indexPath
NSIndexPath * path = [self.tableView indexPathForSelectedRow];

NSString * theList = [rowList objectAtIndex:path.row];
DVC.rowNumber = path.row;
DVC.rowName = theList;


}

您的问题有点令人困惑。 如果要切换到下一种情况,请从按钮上输入rowNumber ++; 在您的button方法中,然后调用您的switch语句所在的方法。

如果我正确理解了这个问题,听起来您正在尝试做与我最近在应用程序中所做的类似的事情。 也就是说,您在导航控制器中拥有一个表格视图,当您选择一个单元格时该表格视图会进入详细视图,然后您希望能够在不同的详细信息视图之间导航而无需返回表格视图来选择另一个单元格。

我使用UIPageViewController做到了这一点。 它的实现有点复杂,但使用起来却很不错。 它的工作方式是在您点击一个单元格时,它会转到一个包含UIPageViewController的视图,然后根据所点击的单元格来加载适当的“页面”。 UIPageViewController处理页面之间的来回移动。 您提供一个充当UIPageViewController数据源的模型控制器。 它的作用是返回给定索引(页码)的详细视图控制器。 要使UIPageViewController为每个页面加载适当的数据,请向其提供与用来填充表格视图相同的数据。

假设您的桌子上有三个单元格。 您点击中间的一个,它将使用UIPageViewController加载视图。 轻触单元格并转换到局部视图时,您可以告诉它是哪个单元格。 然后,UIPageViewController使用此信息来加载正确的详细信息视图。 当用户想要在页面之间导航时,UIPageViewController只需使用与表视图中该单元格相对应的数据加载下一页或上一页的数据。

请注意,UIPageViewController不了解或不关心表视图,它只是使用表视图用于加载其数据的相同源数据(例如数组)。

文档在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM