Iphone:如何将图像或任何内容从第二视图返回到第一视图?

时间:2010-11-29 10:43:09

标签: iphone objective-c xcode uitableview

请查看图片,您将更清楚我想要呈现的内容

这是First View首次推出

这是一个空表视图

右栏上有一个添加按钮

alt text

如果按“添加”按钮,则会进入“第二视图”

第二个是只有3行的表格视图

alt text

点击第一行,它会弹出警报视图,让您设置名称

alt text

按创建,文本将出现在第一行

< 这是问题1,我不知道如何将文字传递给单元格...... >

继续前进,这是第2行和第3行中的自定义单选按钮

除非您先单击单选按钮,否则您无法调用此方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

这是最后两个观点 图标视图1 alt text 图标视图2 alt text

视图中有很多按钮,这是第二个问题: 如果我按下其中一个按钮,它将返回第二个视图并在tableview第2行或第3行显示按钮图标

最后,按完成按钮(右侧栏上相同)

它将返回到第一个视图并显示您键入的名称以及您选择的图标 (选择基础上的单选按钮)

这是最后一个问题。

我在这里上传了我的源代码:Link

你可以下载它......

以下是关于我如何推送视图的代码......

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row==0) {
        [self nameInputAlertView];
    }
    RadioButtonClass *btn = [radioButtonArray objectAtIndex:[indexPath row]];
    if (btn.selected) {
        //Radio Button is selected ,use that icon 
        if (indexPath.row == 1) {
            IconViewOne *iconViewOne = [[IconViewOne alloc]init];
            iconViewOne.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self.navigationController pushViewController:iconViewOne animated:YES];
            [iconViewOne release];
        }
        if (indexPath.row==2) {
            IconViewTwo *iconViewTwo = [[IconViewTwo alloc]init];
            iconViewTwo.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self.navigationController pushViewController:iconViewTwo animated:YES];
            [iconViewTwo release];
        }
    }
}

所以...总结所有问题......

1.如何通过警报视图中的文本字段将文本写入表格视图单元格?

2.如何选择从上一个视图到第二个视图的图标?

3.将所有这两个元素发送到第一个视图:Name&图标

我只知道如何将视图1中的元素传递给视图2 ... 不知道怎么回来......

感谢队友,请帮我弄清楚这个问题。我将非常感谢!

顺便说一句:您可以将单选按钮类添加到任何项目中

3 个答案:

答案 0 :(得分:1)

我不知道这是正确的方法,但我认为这可能会对你有所帮助。 由于您希望在两个视图中检索名称和图像,因此可以在appdelegate中声明它们,以便可以全局访问它。

Que 1:

选择行时,将调用didSelectRow Delegate。在那里你将获得IndexPath。全局存储indexPath。然后

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if ([alertView tag]==1) {
    if (buttonIndex == 1){
        NSLog(@"Created");
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:tableIndexPath]; //tableIndexpath is declared globally as NSindexpath
        ReturnToFirstViewAppDelegate *appObj=(ReturnToFirstViewAppDelegate *)[[UIApplication sharedApplication]delegate];
        appObj.selectedName = macroNameSetting.text; //Setting the Name globally
        cell.detailTextLabel.text =macroNameSetting.text;
        [self.tableView reloadInputViews];
    }
  }
}

Que 2和3

在AppDelegate声明

NSString *selectedName;
UIImage *selectedImage;

分配属性并合成它们

在你的iconViews

- (void)setIcon : (id)sender{
  UIButton *button = (UIButton *)sender;
  UIImage *selectedImg = [UIImage imageNamed:[NSString stringWithFormat:@"marco%d",button.tag]];
    ReturnToFirstViewAppDelegate *appObj=(ReturnToFirstViewAppDelegate *)[[UIApplication sharedApplication]delegate];
    appObj.selectedImage = selectedImg;
// push your viewcontroller
 }

在任何Viewcontroller中将它们作为

进行访问
  ReturnToFirstViewAppDelegate *appObj=(ReturnToFirstViewAppDelegate *)[[UIApplication sharedApplication]delegate];
 //For example
 //cell.image = appObj.selectedImage;

答案 1 :(得分:1)

在任何情况下,如果你不喜欢使用AppDelegate Variables,你可以使用另一种方法来控制导航控制器: -

例如。在“IConViewOne.m”中

UITableViewCell* cell = [[[self.navigationController.viewControllers objectAtIndex:1] tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]] ;
    cell.imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"marco%d.png",button.tag]];

然后在SecondView的“ViewWillAppear”中: -

[self.tableView reloadData];

同样,为了传递给RootViewController你可以给出: -

self.navigationController.topViewController.tableView

OR

[[self.navigationController.viewControllers objectAtIndex:0]tableView]

答案 2 :(得分:0)

将信息从一个对象传递到另一个对象而不知道收件人是谁(并且可能有多个对该信息感兴趣的对象)的好方法是发布NSNotification。它允许您完全避免直接了解其他对象。

阅读Apple的文档Notification Programming Topics并尝试一下。