从 UITableView 发送信息到 SuperView

发布于 2024-12-06 08:13:04 字数 1614 浏览 0 评论 0 原文

我有一个 UITableViewController,在需要时我会务实地调用我的超级视图。当我点击表格视图时,我希望将信息放置在 UITextField 中。现在我可以让它从超级视图正确记录,但文本永远不会被放置在正确的字段中。

来自 UITablViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    NSLog(@"Index %i Touched:",indexPath.row);

    self.MainView = [[TwitterViewController alloc] init];
    self.MainView.view = super.view;

    selectedFriend = [(Tweet*)[friends objectAtIndex:indexPath.row]followingScreenName];

    self.MainView.selectedFriendForTweet = self.selectedFriend;

    [self.MainView setSelectedFriendInBody:self.selectedFriend 
                                      into:self.MainView.TweetBody 
                                      with:self];

    //NSLog(@"selected name on tblview = %@",selectedFriend);
    self.MainView.TweetBody.text = self.selectedFriend;
}

如您所见,当用户点击 tblview 时会调用我的方法

[self.MainView setSelectedFriendInBody:self.selectedFriend 进入:self.MainView.TweetBody 与:自我];

这是该方法:现在该日志可以工作并且信息是正确的,但只是不会进入文本视图!

-(void)setSelectedFriendInBody:(NSString*)aString into:(UITextView*)atextView with:(id)sender
{
    aString = friendsTbl.selectedFriend;
    friendsTbl.selectedFriend = self.selectedFriendForTweet;
    atextView = self.TweetBody;

    [self.TweetBody setText:selectedFriendForTweet];   

    NSLog(@"superviews name = %@", selectedFriendForTweet);
    [selectedFriendForTweet retain];
}

任何帮助将不胜感激!谢谢

I have a UITableViewController that I pragmatically call into my superview when needed. When I tap a table view I want the info to be placed in a UITextField. now I can get it to log correctly from the superview but the text never gets placed in its correct field.

Coming from the UITablViewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    NSLog(@"Index %i Touched:",indexPath.row);

    self.MainView = [[TwitterViewController alloc] init];
    self.MainView.view = super.view;

    selectedFriend = [(Tweet*)[friends objectAtIndex:indexPath.row]followingScreenName];

    self.MainView.selectedFriendForTweet = self.selectedFriend;

    [self.MainView setSelectedFriendInBody:self.selectedFriend 
                                      into:self.MainView.TweetBody 
                                      with:self];

    //NSLog(@"selected name on tblview = %@",selectedFriend);
    self.MainView.TweetBody.text = self.selectedFriend;
}

as you see my method is called when a user taps the tblview

[self.MainView setSelectedFriendInBody:self.selectedFriend
into:self.MainView.TweetBody
with:self];

Here is that method : Now this log Works and the info is correct but just will not go into the textview!

-(void)setSelectedFriendInBody:(NSString*)aString into:(UITextView*)atextView with:(id)sender
{
    aString = friendsTbl.selectedFriend;
    friendsTbl.selectedFriend = self.selectedFriendForTweet;
    atextView = self.TweetBody;

    [self.TweetBody setText:selectedFriendForTweet];   

    NSLog(@"superviews name = %@", selectedFriendForTweet);
    [selectedFriendForTweet retain];
}

Any Help would be greatly appreciated! Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇俏 2024-12-13 08:13:04

你在代码中做了一些奇怪的事情。举个例子:您的方法 setSelectedFriendInBody:into: 获取两个在实现中未使用的参数。更糟糕的是:您为该参数分配了一些值,但这绝对没有效果。一定有什么地方错了……
你的代码是这样的:
a=2; b=3; c=10; d=20;
=> f(x,y)=c+d => f(a,b)=30

并且强制 superview 是一个特殊视图是一个坏主意(就可重用性而言)。正确的方法是使用 delegate-pattern。只需定义一个协议,其中应包含您的方法 setSelectedFriendInBody:into: 并在 MainView 中实现该协议。 TablView 仅获取并调用委托(实现协议的 id)。

@protocol MyTablViewDelegate
-(void)setSelectedFriendInBody:(NSString*)aString;
@end

@interface MyTablView : UITableView<UITableViewDelegate> 
{
    id<MyTablViewDelegate> myDelegate;
}
@property (assign) id<MyTablViewDelegate> myDelegate; //I use assign here for avoiding mem-leaks due to circular retain-chains
@end

@implementation MyTablView 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     ....
     NSString *someString = [(Tweet*)[friends objectAtIndex:indexPath.row]followingScreenName];
     NSLog(@"someString: %@", someString); // maybe there is an error in getting that object from array or in followingScreenName

     [self.myDelegate setSelectedFriendInBody: someString];

}
@end


@interface MainView : UIView<MyTablViewDelegate>
...
@end

@implementation MainView
...
-(void) sthAtLoadTime
{
    MyTablView *mySubView = [[MyTablView alloc] init...];
    mySubView.myDelegate = self;
}
-(void)setSelectedFriendInBody:(NSString*)aString
{
    if(!self.TweetBody)
        NSLog(@"ERR: TweetBody not set");
    self.TeetBody.text = aString;

}
@end

注意另一件事:我假设您的 myTablView 还实现了 UITableViewDelegate 这也不是最好的方法

这应该可以完成工作。

you are doing some strange stuff in your code. Just an example: your method setSelectedFriendInBody:into: gets two parameters which your are not using in your implementation. Even worse: you are assinging some values to that parameters which has definatly no effect. There has to be sth wrong...
your code does sth like this:
a=2; b=3; c=10; d=20;
=> f(x,y)=c+d => f(a,b)=30

And it is a bad idea (with respect to reuseability) to force that the superview is a special view. The right way to do this is to use the delegate-pattern. just define a protocol which should contain your method setSelectedFriendInBody:into: and implement that protocol in your MainView. The TablView only get and call a delegate (an id which implements the protocol).

@protocol MyTablViewDelegate
-(void)setSelectedFriendInBody:(NSString*)aString;
@end

@interface MyTablView : UITableView<UITableViewDelegate> 
{
    id<MyTablViewDelegate> myDelegate;
}
@property (assign) id<MyTablViewDelegate> myDelegate; //I use assign here for avoiding mem-leaks due to circular retain-chains
@end

@implementation MyTablView 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     ....
     NSString *someString = [(Tweet*)[friends objectAtIndex:indexPath.row]followingScreenName];
     NSLog(@"someString: %@", someString); // maybe there is an error in getting that object from array or in followingScreenName

     [self.myDelegate setSelectedFriendInBody: someString];

}
@end


@interface MainView : UIView<MyTablViewDelegate>
...
@end

@implementation MainView
...
-(void) sthAtLoadTime
{
    MyTablView *mySubView = [[MyTablView alloc] init...];
    mySubView.myDelegate = self;
}
-(void)setSelectedFriendInBody:(NSString*)aString
{
    if(!self.TweetBody)
        NSLog(@"ERR: TweetBody not set");
    self.TeetBody.text = aString;

}
@end

Note another thing: I assume that your myTablView also implements the UITableViewDelegate which is also not the best way to do

this should do the work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文