Forum Archive

ObjectC Subview can not disappear by click Done button

alexsun8373

Hi All,
I'm developing a dictionary app for my English test.I want to access the definition of the English word. so
I just add the following code to the button_tapped_event(button_details_tapped) of my project,but when the Apple standard language dictionaries appear, I can not let the sub-view disappear by tapping "Done" button in the sub-view. Can someone help me solve this problem?

The code that I used is:

Coding: utf-8
from objc_util import ObjCClass, UIApplication, CGSize, on_main_thread,ObjCInstance
import sys
import ui

UIReferenceLibraryViewController = ObjCClass('UIReferenceLibraryViewController')

back= ui.View()
back.background_color='gray'
back.name = 'Dictionary'    
back.present('full_screen',hide_title_bar=False)

input = 'word'
referenceViewController = UIReferenceLibraryViewController.alloc().initWithTerm_(input)

ObjCInstance(back).addSubview_(referenceViewController.view())
* list item

rootVC = UIApplication.sharedApplication().keyWindow().rootViewController()
tabVC = rootVC.detailViewController()

referenceViewController.setTitle_('Definition: {0}{1}{0}'.format('\'', input))
referenceViewController.setPreferredContentSize_(CGSize(540, 540))
referenceViewController.setModalPresentationStyle_(2)       
#tabVC.addTabWithViewController_(referenceViewController)
tabVC.presentViewController_animated_completion_(referenceViewController, True, None)

This is part of my project's code:

def button_details_tapped(sender):
    global g_db_element
    global debug
    global v 
    try:
        label_englishshow.text = g_db_element
        UIReferenceLibraryViewController = ObjCClass('UIReferenceLibraryViewController')
        UIViewController = ObjCClass('UIViewController')
        input = g_db_element

        methods = [fun1,fun2,fun3]
        protocols=['ARSCNViewDelegate']
        referenceViewController = create_objc_class('referenceViewController',UIViewController,methods=methods,protocols=protocols)
        referenceViewController = UIReferenceLibraryViewController.alloc().initWithTerm_(input).autorelease()

        ObjCInstance(v).addSubview_(referenceViewController.view())

        rootVC = UIApplication.sharedApplication().keyWindow().rootViewController()
        tabVC = rootVC.detailViewController()

        referenceViewController.setTitle_('Definition: {0}{1}{0}'.format('\'', input))
        referenceViewController.setPreferredContentSize_(CGSize(540, 540))
        referenceViewController.setModalPresentationStyle_(2)       
        #tabVC.addTabWithViewController_(referenceViewController)
        tabVC.presentViewController_animated_completion_(referenceViewController, True, None)

    except Error:
        print(Error)
def button_pronounce_tapped(sender):
    global g_db_element
    try:
        speech.say(g_db_element,'en-UK',0.5)
    except Error:
        print(Error)

def textfield_wordtypein_tapped(sender):
    global g_db_element
    global debug
    try:
        label_englishshow.text = g_db_element
        if debug == True:
            print("textfield_wordtypein_tapped function")
    except Error:
        print(Error)

def textfield_chapter_tapped(sender):
    global  g_db_chapter
    try:
        g_db_chapter = int(sender.text)
    except Error:
        print(Error)

def textfield_section_tapped(sender):
    global g_db_section
    try:
        g_db_section = int(sender.text)
    except Error:
        print(Error)


v = ui.load_view()
label_englishshow = v['label_englishshow']
label_chinese_meaningshow = v['label_chinese&meaningshow']
textfield_wordtypein = v['textfield_wordtypein']


textfield_chapter = v['textfield_chapter']
textfield_section = v['textfield_section']
textfield_chapter.text = str(g_db_chapter)
textfield_section.text = str(g_db_section)
#get the row length of database
conn = sqlite3.connect('wanglu_linguistic_material.db')
cursor = conn.cursor()
cursor.execute("select * from wanglu_linguistic_material")
results = cursor.fetchall() 
G_DB_ROW_LENGTH = len(results)
conn.close
#end
v.present('sheet')

The whole code is in github:
https://github.com/alexsun8373/pythonista_wanglu_linguistic_material/blob/master/wanglu_linguistic_material.py

Best regards!

cvp

@alexsun8373 Try

@on_main_thread 
def button_details_tapped(sender):
cvp

@alexsun8373 this works, it I think you need to remove first Pythonista from active apps because your initial script has created (unused) class

Forget variables input and v, dirty Python but only to show 😀

from objc_util import *
import ui

UIReferenceLibraryViewController = ObjCClass('UIReferenceLibraryViewController')

v = ui.View()
v.background_color='gray'
v.name = 'Dictionary'    
v.present('full_screen',hide_title_bar=False)

@on_main_thread 
def button_details_tapped(sender):
    global v
    try:
        input = 'word'
        referenceViewController = UIReferenceLibraryViewController.alloc().initWithTerm_(input).autorelease()

        objc_uiview = ObjCInstance(v)
        SUIViewController = ObjCClass('SUIViewController')
        vc = SUIViewController.viewControllerForView_(objc_uiview)      

        referenceViewController.setTitle_('Definition: {0}{1}{0}'.format('\'', input))
        referenceViewController.setPreferredContentSize_(CGSize(540, 540))
        referenceViewController.setModalPresentationStyle_(2)       
        vc.presentViewController_animated_completion_(referenceViewController, True, None)

    except Exception as e:
        print(e)

b = ui.ButtonItem()
b.title = 'tap'
b.action = button_details_tapped 
v.left_button_items = (b,)

input = 'word'
alexsun8373

@cvp It worked for me, thank you so much!