开发者

How to get an url from textfield and use it in the application?

开发者 https://www.devze.com 2023-02-20 10:06 出处:网络
I have been working on a easy RSS reader for my own. I am new to Objective-C so I am stuck at one point. There is a add button on the navigation bar of the application which opens a uialertview with a

I have been working on a easy RSS reader for my own. I am new to Objective-C so I am stuck at one point. There is a add button on the navigation bar of the application which opens a uialertview with a textfield where the user gonna input the url of the rss feed. The application has a predefined rss feed address in its source code. I am writing my code examples below:

- (void)viewDidLoad {
    [super viewDidLoad];    
    self.feeds = [NSArray arrayWithObjects:@"http://www.formula1.com/rss/news/latest.rss",
                  nil];

fe开发者_高级运维eds is the predefined array which has the address of the feed. I tried that code below to get the text but it didnt work. Also how the application gonna take that url from textfield and replace with the predefined one?

- (void) myAlertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex == 1)

    { // Ok pushed
        //[data addObject:myTextField.text];

    }


The buttons in a UIAlertView are zero-index. If OK is your only button, is should be:

if (buttonIndex == 0)

Also, if I understand you correctly, you want to replace the text with the array property. The answer would depend on where the textfield is but I recommend you give a tag to your textfield as so:

myTextField.tag = 10; //Any number should work

Now you can access that textfield using the method viewWithTag: on it's parent view.

If the textfield is in the main view, you would retrieve as follows (within your ViewController):

UITextField *textfield = (UITextField *)[self.view viewWithTag:tagNumber]; //tagNumber being the tag you assigned to the field
textfield.text = [self.feeds objectAtIndex:0];

It would be similar if the field was in the address book (within the clickedButtonAtIndex method): UITextField *textfield = (UITextField *)[alert viewWithTag:tagNumber]; //tagNumber being the tag you assigned to the field textfield.text = [self.feeds objectAtIndex:0];

EDIT: I reread what you've written and have understood the question better. I'm unsure why you're storing the URL in an array (unless you plan to allow them to enter multiple arrays).

If so, you really don't need a predefined, but to remove it you can do the following in the clickedButtonAtIndex method:

[self.feeds removeAllObjects];
[self.feeds addObject:textfield.text];

That should remove your predefined string and add the new one. enter code here

0

精彩评论

暂无评论...
验证码 换一张
取 消