如何在标签栏中设置标记值?

时间:2022-08-26 21:35:18

i have to read datas from xml and have to display values in view controller. am using tab barcontroller . where i have to read xml data and have to display it in tab bar as badge. here is my controller code.

我必须从xml读取数据,并且必须在视图控制器中显示值。使用tab barcontroller。我需要读取xml数据并将其显示在标签栏中作为标记。这是我的控制器代码。

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{

    if([elementName isEqualToString:@"Event"]) {
        //Initialize the array.
        rssOutputData  = [[NSMutableArray alloc] init];
        mycount = [attributeDict objectForKey:@"count"];
         NSLog(@"Reading count value :%@",mycount);
    }
    else if([elementName isEqualToString:@"eventdashboard"]) {
        //Initialize the book.
        aEvent = [[Eventlist alloc] init];
        //Extract the attribute here.
        aEvent.id = [[attributeDict objectForKey:@"userid"] integerValue];
        NSLog(@"Reading id value :%d", aEvent.id);
    }
    NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if(!nodecontent)
        nodecontent = [[NSMutableString alloc] initWithString:string];else
            [nodecontent appendString:string];
    NSLog(@"Processing Value: %@", nodecontent);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"Event"])
        return;
    if([elementName isEqualToString:@"eventdashboard"]) {
        [rssOutputData addObject:aEvent];
        [aEvent release];
        aEvent= nil;
        }
    else
        [aEvent setValue:nodecontent forKey:elementName];
    [nodecontent release];
    nodecontent = nil;

}
- (void)viewDidLoad
{

        [super viewDidLoad];
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg2.jpeg"]];
        NSLog(@"%d",memberid);
        myid = [SingleTonClass sinlgeTon].memberIdOne;
        NSString *post =[[NSString alloc] initWithFormat:@"userid=%d",myid];
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
        NSURL *url = [NSURL URLWithString:@"http://journalonline.in/cfts/dashboards/dashboard?"];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
        // [theRequest addRequestHeader:@"Content-Type" value:@"application/xml"];
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
        [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [theRequest setHTTPBody:postData];
        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
        if( theConnection )
        {
            webData = [[NSMutableData data] retain];
            NSLog(@"%@",webData);
        }
        }

-(void)viewWillAppear:(BOOL)animated{
    [tblView reloadData];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [connection release];
    [webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    eventarray = [[NSMutableArray alloc]init];
    xmlParserObject = [[NSXMLParser alloc] initWithData:webData];
    [xmlParserObject setDelegate:self];
    [xmlParserObject parse];


     for (int i =0; i<[rssOutputData count]; i++) {
        Eventlist *log = [rssOutputData objectAtIndex:i];
        eventid = log.id;
        NSLog(@"%d",eventid);
        Invit = log.invitationdet; 
        NSLog(@"%@",Invit);

        [eventarray addObject:log];
    }


    [connection release];
    [tblView reloadData];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSString * segueIdentifier = [segue identifier];
    if([segueIdentifier isEqualToString:@"MainEventList"]){
        UINavigationController *nav = [segue destinationViewController];
        MaineventListController *MVC = (MaineventListController *)nav.topViewController;
        MVC.memberid1 = [SingleTonClass sinlgeTon].memberIdOne;
        NSLog(@"%d",MVC.memberid1);
    }
    if ([segue.identifier isEqualToString:@"EventListDetail"])
    {
        NSIndexPath *indexPath = [tblView indexPathForSelectedRow];
        [[SingleTonClass sinlgeTon] addObjectsColorArray:[NSString stringWithFormat:@"%d",indexPath.row]];
        MainEventDetailController *destViewController = segue.destinationViewController;
        MainEvent *aEvent1 = [eventarray objectAtIndex:indexPath.row];
        destViewController.eveID = aEvent1.id;
        destViewController.usrid =  [SingleTonClass sinlgeTon].memberIdOne;
    }
}

-(void)didReceiveMemoryWarning  {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [eventarray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0)
        return  [eventarray count];
    if (section == 1)
        return 1;
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"eventCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(indexPath.section == 0)
    {
        Eventlist *msglist = [eventarray objectAtIndex:indexPath.row];
        cell.textLabel.text = msglist.invitationdet;
        NSLog(@"Array %@",[SingleTonClass sinlgeTon].colorArray);
        NSInteger stat=msglist.readflag;
        if([[SingleTonClass sinlgeTon].colorArray containsObject:[NSString stringWithFormat:@"%d",indexPath.row]] || stat == 1 ) {
            NSInteger stat1 = msglist.responseflag;
            if(stat1 == 1){
                cell.textLabel.textColor = [UIColor yellowColor];
            }
            else {
             cell.textLabel.textColor = [UIColor redColor];
            }
        }
               else{
        cell.textLabel.textColor = [UIColor greenColor];
        }
        cell.backgroundColor = [UIColor blackColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
          }
    if(indexPath.section == 1)
    {
        UIButton *viewmoreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        viewmoreButton.frame = CGRectMake(200.0f, 5.0f, 80.0f, 30.0f);
        [viewmoreButton setTitle:@"View More" forState:UIControlStateNormal]; 
        [cell addSubview:viewmoreButton];
        [viewmoreButton addTarget:self
                           action:@selector(viewMore:)
                 forControlEvents:UIControlEventTouchUpInside];
        cell.backgroundColor = [ UIColor blackColor];
    }
    return cell;
}
- (void)viewMore:(id)sender
{
    [self performSegueWithIdentifier:@"MainEventList" sender:sender];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void) dealloc
{
    [xmlParserObject release];
    [super dealloc];
}

Here am reading a value from XML

这里正在读取XML中的值

 mycount = [attributeDict objectForKey:@"count"];

I dont know to display in tab bar as badge. i tried to display it under ViewdidLoad but didnt worked pls help me friends

我不知道在标签栏中显示徽章。我尝试在ViewdidLoad下显示它,但没有工作,请帮助我的朋友

8 个解决方案

#1


65  

Let me tell you the simplest way to set badge value :

让我告诉你设置徽章值的最简单方法:

[[self navigationController] tabBarItem].badgeValue = @"YourBadgeValue";

#2


48  

Vineet Singh's answer is good if you want to change the badge for the current tab. If instead you need to change the value for another tab you have in your tab bar, use the following:

如果您想要更改当前选项卡的面板,Vineet Singh的答案是好的。如果您需要更改选项卡栏中另一个选项卡的值,请使用以下方法:

Objective-C

objective - c

[[self.tabBarController.tabBar.items objectAtIndex:0] setBadgeValue:nil];

Swift

斯威夫特

tabBarController?.tabBar.items?[0].badgeValue = nil

Remember to replace the index I used with the actual tab you want to edit the value for.

记住要用要编辑值的实际选项卡替换我使用的索引。

#3


4  

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
UITabBarItem *tbItem = (UITabBarItem *)[appDelegate.tabBarController.tabBar.items objectAtIndex:tabIndex];
tbItem.badgeValue = [NSString stringWithFormat:@"%i", count];

#4


1  

I see some answers about setting this on the TabItem, but if the tab has never been loaded the viewDidLoad function will never get loaded. This is especially important when you are trying to create a background process such as push notification handling set a tab's badge. It's much more likely you will need your TabViewController to handle the badge setting. Tags on the TabItem seem more appropriate and then flip through the tabs until you find the one you want to update.

我在TabItem上看到一些关于设置这个的答案,但是如果标签从来没有加载,viewDidLoad函数将永远不会被加载。当您尝试创建一个后台进程时,这一点尤为重要,比如push notification handling set a tab's badge。你更可能需要你的TabViewController来处理徽章设置。选项卡项上的标签似乎更合适,然后浏览选项卡,直到找到要更新的选项卡。

Cheers!

干杯!

#5


1  

Try this:

试试这个:

tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",count];

#6


1  

swift 2.0:

斯威夫特2.0:

    self.navigationController!.tabBarItem.badgeValue = "YourBadgeValue"

#7


0  

int indexOfTabbarItem = 0;
    NSString *badgeValue = [NSString stringWithFormat:@"%lu", (long)countUnreadMessages];

    [[[[self viewControllers] objectAtIndex:indexOfTabbarItem] tabBarItem] setBadgeValue:([badgeValue isEqualToString:@"0"]) ? nil : badgeValue];

This worked for me. Some times you have to find the right controller and then its item to set it. Cheers.

这为我工作。有时你必须找到正确的控制器,然后再设置它的项。欢呼。

#8


0  

For ionic angular, you just bind the value to tabBadge

对于离子角度,你只需将值绑定到tabBadge上

  <ion-tab [root]="tab3Root" tabIcon="notifications" tabBadgeStyle="red" tabBadge={{totalUnread}}></ion-tab>

#1


65  

Let me tell you the simplest way to set badge value :

让我告诉你设置徽章值的最简单方法:

[[self navigationController] tabBarItem].badgeValue = @"YourBadgeValue";

#2


48  

Vineet Singh's answer is good if you want to change the badge for the current tab. If instead you need to change the value for another tab you have in your tab bar, use the following:

如果您想要更改当前选项卡的面板,Vineet Singh的答案是好的。如果您需要更改选项卡栏中另一个选项卡的值,请使用以下方法:

Objective-C

objective - c

[[self.tabBarController.tabBar.items objectAtIndex:0] setBadgeValue:nil];

Swift

斯威夫特

tabBarController?.tabBar.items?[0].badgeValue = nil

Remember to replace the index I used with the actual tab you want to edit the value for.

记住要用要编辑值的实际选项卡替换我使用的索引。

#3


4  

AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
UITabBarItem *tbItem = (UITabBarItem *)[appDelegate.tabBarController.tabBar.items objectAtIndex:tabIndex];
tbItem.badgeValue = [NSString stringWithFormat:@"%i", count];

#4


1  

I see some answers about setting this on the TabItem, but if the tab has never been loaded the viewDidLoad function will never get loaded. This is especially important when you are trying to create a background process such as push notification handling set a tab's badge. It's much more likely you will need your TabViewController to handle the badge setting. Tags on the TabItem seem more appropriate and then flip through the tabs until you find the one you want to update.

我在TabItem上看到一些关于设置这个的答案,但是如果标签从来没有加载,viewDidLoad函数将永远不会被加载。当您尝试创建一个后台进程时,这一点尤为重要,比如push notification handling set a tab's badge。你更可能需要你的TabViewController来处理徽章设置。选项卡项上的标签似乎更合适,然后浏览选项卡,直到找到要更新的选项卡。

Cheers!

干杯!

#5


1  

Try this:

试试这个:

tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",count];

#6


1  

swift 2.0:

斯威夫特2.0:

    self.navigationController!.tabBarItem.badgeValue = "YourBadgeValue"

#7


0  

int indexOfTabbarItem = 0;
    NSString *badgeValue = [NSString stringWithFormat:@"%lu", (long)countUnreadMessages];

    [[[[self viewControllers] objectAtIndex:indexOfTabbarItem] tabBarItem] setBadgeValue:([badgeValue isEqualToString:@"0"]) ? nil : badgeValue];

This worked for me. Some times you have to find the right controller and then its item to set it. Cheers.

这为我工作。有时你必须找到正确的控制器,然后再设置它的项。欢呼。

#8


0  

For ionic angular, you just bind the value to tabBadge

对于离子角度,你只需将值绑定到tabBadge上

  <ion-tab [root]="tab3Root" tabIcon="notifications" tabBadgeStyle="red" tabBadge={{totalUnread}}></ion-tab>