【问题标题】:How to set badge value in Tab bar?如何在标签栏中设置徽章值?
【发布时间】:2013-12-01 09:36:07
【问题描述】:

我必须从 xml 读取数据并且必须在视图控制器中显示值。我正在使用标签栏控制器。我必须读取 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];
}

这里是从 XML 读取一个值

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

我不知道在标签栏中显示为徽章。我试图在 ViewdidLoad 下显示它但没有用 请朋友们帮帮我

【问题讨论】:

    标签: ios objective-c xml uitabbar


    【解决方案1】:

    如果您想更改当前标签的徽章,Vineet Singh 的回答很好。如果您需要更改标签栏中另一个标签的值,请使用以下命令:

    Objective-C

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

    斯威夫特

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

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

    【讨论】:

    • 您应该将其设置为nil 而不是@"",因为这样做仍然会显示红色气泡。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      斯威夫特 2.0:

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

      【讨论】:

        【解决方案4】:
        int indexOfTabbarItem = 0;
            NSString *badgeValue = [NSString stringWithFormat:@"%lu", (long)countUnreadMessages];
        
            [[[[self viewControllers] objectAtIndex:indexOfTabbarItem] tabBarItem] setBadgeValue:([badgeValue isEqualToString:@"0"]) ? nil : badgeValue];
        

        这对我有用。有时您必须找到正确的控制器,然后找到它的项目来设置它。干杯。

        【讨论】:

          【解决方案5】:

          试试这个:

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

          【讨论】:

          • iOS 9.2/Swift 2.1.1/Xcode 7.2.1/OSX 10.10.5tabBarItem 是 View Controller 的一个属性,将这条线放在 View Controller 中的某个位置,比如在 viewWillAppear() 中,对我有用。
          【解决方案6】:

          我看到了一些关于在 TabItem 上设置此项的答案,但是如果从未加载过该选项卡,则永远不会加载 viewDidLoad 函数。当您尝试创建后台进程(例如推送通知处理设置选项卡的标记)时,这一点尤其重要。您更有可能需要 TabViewController 来处理徽章设置。 TabItem 上的标签似乎更合适,然后翻阅标签,直到找到要更新的标签。

          干杯!

          【讨论】:

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

            【讨论】:

              【解决方案8】:

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

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

              【讨论】:

              • 在控制器的哪里添加这个?
              • 您需要在要设置徽章值的特定选项卡的视图控制器上添加它。
              • 在我的标签栏出现之前我应该​​如何设置徽章值?
              • 知道如何在 Xamarin Forms Crossplatform App 中实现这一点吗?
              • viewDidLoad() 函数中的@user2728139
              猜你喜欢
              • 1970-01-01
              • 2017-08-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-10-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多